Why does TypeScript pack a Class in an IIFE?How to change an element's class with JavaScript?How does JavaScript .prototype work?How can I select an element with multiple classes in jQuery?What does “use strict” do in JavaScript, and what is the reasoning behind it?What is JSONP, and why was it created?Why does Google prepend while(1); to their JSON responses?Why does ++[[]][+[]]+[+[]] return the string “10”?How does data binding work in AngularJS?What is TypeScript and why would I use it in place of JavaScript?TypeScript Converting a String to a number
Increase height of laser cut design file for enclosure
"Right on the tip of my tongue" meaning?
How to make the table in the figure in LaTeX?
Why in a Ethernet LAN, a packet sniffer can obtain all packets sent over the LAN?
How do I get past a 3-year ban from overstay with VWP?
Would an 8% reduction in drag outweigh the weight addition from this custom CFD-tested winglet?
How can a Lich look like a human without magic?
Ubuntu won't let me edit or delete .vimrc file
How old is Captain America at the end of "Avengers: Endgame"?
What does it mean with the ask price is below the last price?
Limit of an integral vs Limit of the integrand
Understanding basic photoresistor circuit
Adding slope values to attribute table (QGIS 3)
Was there ever any real use for a 6800-based Apple I?
What did Rocket give Hawkeye in "Avengers: Endgame"?
Drawing Quarter-Circle
Are there variations of the regular runtimes of the Big-O-Notation?
Is the homebrew weapon attack cantrip 'Arcane Strike' balanced?
How to make a language evolve quickly?
find not returning expected files
Why do Thanos's punches not kill Captain America or at least cause some mortal injuries?
How did Thanos not realise this had happened at the end of Endgame?
Was the Highlands Ranch shooting the 115th mass shooting in the US in 2019
How do I compare the result of "1d20+x, with advantage" to "1d20+y, without advantage", assuming x < y?
Why does TypeScript pack a Class in an IIFE?
How to change an element's class with JavaScript?How does JavaScript .prototype work?How can I select an element with multiple classes in jQuery?What does “use strict” do in JavaScript, and what is the reasoning behind it?What is JSONP, and why was it created?Why does Google prepend while(1); to their JSON responses?Why does ++[[]][+[]]+[+[]] return the string “10”?How does data binding work in AngularJS?What is TypeScript and why would I use it in place of JavaScript?TypeScript Converting a String to a number
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Here is a TypeScript class:
class Greeter
public static What(): string
return "Greater";
public subject: string;
constructor(subject: string)
this.subject = subject;
public greet(): string
return "Hello, " + this.subject;
It is transpiled to IIFE:
var Greeter = /** @class */ (function ()
function Greeter(subject)
this.subject = subject;
Greeter.What = function ()
return "Greater";
;
Greeter.prototype.greet = function ()
return "Hello, " + this.subject;
;
return Greeter;
());
However, it generally works in the same way when it is presented as a Constructor function. Which, of course, looks more JavaScriptish and handwritten :)
function Greeter(subject)
this.subject = subject;
Greeter.What = function ()
return "Greater";
;
Greeter.prototype.greet = function ()
return "Hello, " + this.subject;
;
What is the benefit or motives to pack it in IIFE?
I made a naive benchmark:
console.time("Greeter");
for(let i = 0; i < 100000000; i++)
new Greeter("world" + i);
console.timeEnd("Greeter");
It showed virtually the same instantiation speed. Of course, we cannot expect any difference because the IIFE is resolved only once.
Edit:
I was thinking that maybe it is because of closure, but the iife doesn't take arguments. It must not be a closure.
javascript typescript iife
add a comment |
Here is a TypeScript class:
class Greeter
public static What(): string
return "Greater";
public subject: string;
constructor(subject: string)
this.subject = subject;
public greet(): string
return "Hello, " + this.subject;
It is transpiled to IIFE:
var Greeter = /** @class */ (function ()
function Greeter(subject)
this.subject = subject;
Greeter.What = function ()
return "Greater";
;
Greeter.prototype.greet = function ()
return "Hello, " + this.subject;
;
return Greeter;
());
However, it generally works in the same way when it is presented as a Constructor function. Which, of course, looks more JavaScriptish and handwritten :)
function Greeter(subject)
this.subject = subject;
Greeter.What = function ()
return "Greater";
;
Greeter.prototype.greet = function ()
return "Hello, " + this.subject;
;
What is the benefit or motives to pack it in IIFE?
I made a naive benchmark:
console.time("Greeter");
for(let i = 0; i < 100000000; i++)
new Greeter("world" + i);
console.timeEnd("Greeter");
It showed virtually the same instantiation speed. Of course, we cannot expect any difference because the IIFE is resolved only once.
Edit:
I was thinking that maybe it is because of closure, but the iife doesn't take arguments. It must not be a closure.
javascript typescript iife
I'd guess it's a way to keep the transpiled class body encapsulated in the class-like block, though I think that would only be useful for debugging (and it's better to read the source code than the transpiled code)
– CertainPerformance
1 hour ago
Not consideringprivate
properties either. No way to do those without IIFE since javascript itself has no such construct
– charlietfl
1 hour ago
add a comment |
Here is a TypeScript class:
class Greeter
public static What(): string
return "Greater";
public subject: string;
constructor(subject: string)
this.subject = subject;
public greet(): string
return "Hello, " + this.subject;
It is transpiled to IIFE:
var Greeter = /** @class */ (function ()
function Greeter(subject)
this.subject = subject;
Greeter.What = function ()
return "Greater";
;
Greeter.prototype.greet = function ()
return "Hello, " + this.subject;
;
return Greeter;
());
However, it generally works in the same way when it is presented as a Constructor function. Which, of course, looks more JavaScriptish and handwritten :)
function Greeter(subject)
this.subject = subject;
Greeter.What = function ()
return "Greater";
;
Greeter.prototype.greet = function ()
return "Hello, " + this.subject;
;
What is the benefit or motives to pack it in IIFE?
I made a naive benchmark:
console.time("Greeter");
for(let i = 0; i < 100000000; i++)
new Greeter("world" + i);
console.timeEnd("Greeter");
It showed virtually the same instantiation speed. Of course, we cannot expect any difference because the IIFE is resolved only once.
Edit:
I was thinking that maybe it is because of closure, but the iife doesn't take arguments. It must not be a closure.
javascript typescript iife
Here is a TypeScript class:
class Greeter
public static What(): string
return "Greater";
public subject: string;
constructor(subject: string)
this.subject = subject;
public greet(): string
return "Hello, " + this.subject;
It is transpiled to IIFE:
var Greeter = /** @class */ (function ()
function Greeter(subject)
this.subject = subject;
Greeter.What = function ()
return "Greater";
;
Greeter.prototype.greet = function ()
return "Hello, " + this.subject;
;
return Greeter;
());
However, it generally works in the same way when it is presented as a Constructor function. Which, of course, looks more JavaScriptish and handwritten :)
function Greeter(subject)
this.subject = subject;
Greeter.What = function ()
return "Greater";
;
Greeter.prototype.greet = function ()
return "Hello, " + this.subject;
;
What is the benefit or motives to pack it in IIFE?
I made a naive benchmark:
console.time("Greeter");
for(let i = 0; i < 100000000; i++)
new Greeter("world" + i);
console.timeEnd("Greeter");
It showed virtually the same instantiation speed. Of course, we cannot expect any difference because the IIFE is resolved only once.
Edit:
I was thinking that maybe it is because of closure, but the iife doesn't take arguments. It must not be a closure.
javascript typescript iife
javascript typescript iife
edited 1 hour ago
Miroslav Popov
asked 1 hour ago
Miroslav PopovMiroslav Popov
1,30211936
1,30211936
I'd guess it's a way to keep the transpiled class body encapsulated in the class-like block, though I think that would only be useful for debugging (and it's better to read the source code than the transpiled code)
– CertainPerformance
1 hour ago
Not consideringprivate
properties either. No way to do those without IIFE since javascript itself has no such construct
– charlietfl
1 hour ago
add a comment |
I'd guess it's a way to keep the transpiled class body encapsulated in the class-like block, though I think that would only be useful for debugging (and it's better to read the source code than the transpiled code)
– CertainPerformance
1 hour ago
Not consideringprivate
properties either. No way to do those without IIFE since javascript itself has no such construct
– charlietfl
1 hour ago
I'd guess it's a way to keep the transpiled class body encapsulated in the class-like block, though I think that would only be useful for debugging (and it's better to read the source code than the transpiled code)
– CertainPerformance
1 hour ago
I'd guess it's a way to keep the transpiled class body encapsulated in the class-like block, though I think that would only be useful for debugging (and it's better to read the source code than the transpiled code)
– CertainPerformance
1 hour ago
Not considering
private
properties either. No way to do those without IIFE since javascript itself has no such construct– charlietfl
1 hour ago
Not considering
private
properties either. No way to do those without IIFE since javascript itself has no such construct– charlietfl
1 hour ago
add a comment |
2 Answers
2
active
oldest
votes
It's done to preserve native class behavior in edge cases like this, where someone tries to use class Greeter
before it's defined:
// this is javascript code, not TypeScript
console.log(Greeter.What());
class Greeter
Greeter.What = function What()
return "Greater";
With native class implementation, this should print ReferenceError: Greeter is not defined
.
When transpiled and wrapped in IIFE, the result is the same when executed in strict
mode.
Without IIFE, unwrapped function is hoisted and name Greeter
is in scope before it's defined, so the different error is produced: TypeError: Greeter.What is not a function
Note that IIFE is not used to hide private instance or class properties because it's not necessary anyway. When transpiled, instance properties are assigned as properties for this
inside the constructor, and static properties are assigned as properties of Greeter
object - no variables are created.
Avar
won't get hoisted
– charlietfl
37 mins ago
add a comment |
TypeScript will pass arguments to the IIFE in cases where there is inheritance between classes. For example, the closure below is used when Greeter
extends a BaseGreeter
class:
var Greeter = /** @class */ (function (_super)
// __extends is added by the TS transpiler to simulate inheritance
__extends(Greeter, _super);
function Greeter(subject)
Greeter.What = function ()
return "Greater";
;
Greeter.prototype.greet = function ()
return "Hello, " + this.subject;
;
return Greeter;
(BaseGreeter));
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56086411%2fwhy-does-typescript-pack-a-class-in-an-iife%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
It's done to preserve native class behavior in edge cases like this, where someone tries to use class Greeter
before it's defined:
// this is javascript code, not TypeScript
console.log(Greeter.What());
class Greeter
Greeter.What = function What()
return "Greater";
With native class implementation, this should print ReferenceError: Greeter is not defined
.
When transpiled and wrapped in IIFE, the result is the same when executed in strict
mode.
Without IIFE, unwrapped function is hoisted and name Greeter
is in scope before it's defined, so the different error is produced: TypeError: Greeter.What is not a function
Note that IIFE is not used to hide private instance or class properties because it's not necessary anyway. When transpiled, instance properties are assigned as properties for this
inside the constructor, and static properties are assigned as properties of Greeter
object - no variables are created.
Avar
won't get hoisted
– charlietfl
37 mins ago
add a comment |
It's done to preserve native class behavior in edge cases like this, where someone tries to use class Greeter
before it's defined:
// this is javascript code, not TypeScript
console.log(Greeter.What());
class Greeter
Greeter.What = function What()
return "Greater";
With native class implementation, this should print ReferenceError: Greeter is not defined
.
When transpiled and wrapped in IIFE, the result is the same when executed in strict
mode.
Without IIFE, unwrapped function is hoisted and name Greeter
is in scope before it's defined, so the different error is produced: TypeError: Greeter.What is not a function
Note that IIFE is not used to hide private instance or class properties because it's not necessary anyway. When transpiled, instance properties are assigned as properties for this
inside the constructor, and static properties are assigned as properties of Greeter
object - no variables are created.
Avar
won't get hoisted
– charlietfl
37 mins ago
add a comment |
It's done to preserve native class behavior in edge cases like this, where someone tries to use class Greeter
before it's defined:
// this is javascript code, not TypeScript
console.log(Greeter.What());
class Greeter
Greeter.What = function What()
return "Greater";
With native class implementation, this should print ReferenceError: Greeter is not defined
.
When transpiled and wrapped in IIFE, the result is the same when executed in strict
mode.
Without IIFE, unwrapped function is hoisted and name Greeter
is in scope before it's defined, so the different error is produced: TypeError: Greeter.What is not a function
Note that IIFE is not used to hide private instance or class properties because it's not necessary anyway. When transpiled, instance properties are assigned as properties for this
inside the constructor, and static properties are assigned as properties of Greeter
object - no variables are created.
It's done to preserve native class behavior in edge cases like this, where someone tries to use class Greeter
before it's defined:
// this is javascript code, not TypeScript
console.log(Greeter.What());
class Greeter
Greeter.What = function What()
return "Greater";
With native class implementation, this should print ReferenceError: Greeter is not defined
.
When transpiled and wrapped in IIFE, the result is the same when executed in strict
mode.
Without IIFE, unwrapped function is hoisted and name Greeter
is in scope before it's defined, so the different error is produced: TypeError: Greeter.What is not a function
Note that IIFE is not used to hide private instance or class properties because it's not necessary anyway. When transpiled, instance properties are assigned as properties for this
inside the constructor, and static properties are assigned as properties of Greeter
object - no variables are created.
answered 50 mins ago
artemartem
16.5k33140
16.5k33140
Avar
won't get hoisted
– charlietfl
37 mins ago
add a comment |
Avar
won't get hoisted
– charlietfl
37 mins ago
A
var
won't get hoisted– charlietfl
37 mins ago
A
var
won't get hoisted– charlietfl
37 mins ago
add a comment |
TypeScript will pass arguments to the IIFE in cases where there is inheritance between classes. For example, the closure below is used when Greeter
extends a BaseGreeter
class:
var Greeter = /** @class */ (function (_super)
// __extends is added by the TS transpiler to simulate inheritance
__extends(Greeter, _super);
function Greeter(subject)
Greeter.What = function ()
return "Greater";
;
Greeter.prototype.greet = function ()
return "Hello, " + this.subject;
;
return Greeter;
(BaseGreeter));
add a comment |
TypeScript will pass arguments to the IIFE in cases where there is inheritance between classes. For example, the closure below is used when Greeter
extends a BaseGreeter
class:
var Greeter = /** @class */ (function (_super)
// __extends is added by the TS transpiler to simulate inheritance
__extends(Greeter, _super);
function Greeter(subject)
Greeter.What = function ()
return "Greater";
;
Greeter.prototype.greet = function ()
return "Hello, " + this.subject;
;
return Greeter;
(BaseGreeter));
add a comment |
TypeScript will pass arguments to the IIFE in cases where there is inheritance between classes. For example, the closure below is used when Greeter
extends a BaseGreeter
class:
var Greeter = /** @class */ (function (_super)
// __extends is added by the TS transpiler to simulate inheritance
__extends(Greeter, _super);
function Greeter(subject)
Greeter.What = function ()
return "Greater";
;
Greeter.prototype.greet = function ()
return "Hello, " + this.subject;
;
return Greeter;
(BaseGreeter));
TypeScript will pass arguments to the IIFE in cases where there is inheritance between classes. For example, the closure below is used when Greeter
extends a BaseGreeter
class:
var Greeter = /** @class */ (function (_super)
// __extends is added by the TS transpiler to simulate inheritance
__extends(Greeter, _super);
function Greeter(subject)
Greeter.What = function ()
return "Greater";
;
Greeter.prototype.greet = function ()
return "Hello, " + this.subject;
;
return Greeter;
(BaseGreeter));
answered 40 mins ago
PeterPeter
511410
511410
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56086411%2fwhy-does-typescript-pack-a-class-in-an-iife%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I'd guess it's a way to keep the transpiled class body encapsulated in the class-like block, though I think that would only be useful for debugging (and it's better to read the source code than the transpiled code)
– CertainPerformance
1 hour ago
Not considering
private
properties either. No way to do those without IIFE since javascript itself has no such construct– charlietfl
1 hour ago