javascript - Typescript generating redundant variable -
consider following typescript code:
module demoappmodule{ 'use strict'; export module nest{ export var hello = function () { alert('hello!'); }; } } demoappmodule.nest.hello();
after transpiling have following javascript code:
var demoappmodule; (function (demoappmodule) { 'use strict'; (function (nest) { nest.hello = function () { alert('hello!'); }; })(demoappmodule.nest || (demoappmodule.nest = {})); var nest = demoappmodule.nest; })(demoappmodule || (demoappmodule = {})); demoappmodule.nest.hello();
why line generated? hurts eyes.
var nest = demoappmodule.nest;
short answer: needed access module variable locally. e.g.
module demoappmodule{ 'use strict'; export module nest{ export var hello = function () { alert('hello!'); }; } // following not possible without line console.log(nest.hello); } demoappmodule.nest.hello();
longer answer: similar var added before module e.g. notice var x
:
// typescript module x{export var foo;} // generated javascript var x; (function (x) { x.foo; })(x || (x = {}));
but when inside module + export module var
needs added outermodule.innermodule
not var innermodule
upfront. add outermodule
, create local variable point innermodule
can see in generated javascript:
// notice var here var demoappmodule; (function (demoappmodule) { 'use strict'; // notice no var here (function (nest) { nest.hello = function () { alert('hello!'); }; })(demoappmodule.nest || (demoappmodule.nest = {})); // notice var assinged afterwards var nest = demoappmodule.nest; // following not possible without line console.log(nest.hello); })(demoappmodule || (demoappmodule = {})); demoappmodule.nest.hello();
Comments
Post a Comment