-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support 'new.target' meta object #2551
Comments
We also need to update es6.d.ts for
Where |
See relevant down-level emit suggestions in #8494 |
For completeness I will just copy them here: Given: function Foo() {
console.log(new.target);
}
Foo(); // logs undefined
new Foo(); // logs Foo The downlevel emit could be: function Foo() {
var _newTarget = this === (typeof window === 'undefined' ? global : window) ? undefined : this && this.constructor;
console.log(_newTarget);
}
Foo(); // logs undefined
new Foo(); // logs Foo When 'use strict';
function Foo() {
var _newTarget = this && this.constructor;
console.log(_newTarget);
}
Foo(); // logs undefined
new Foo(); // logs Foo |
Just happened across this thread while looking at TS support for 'new.target'. For use case of importing a function such as the Foo example above when such function is exported from, say, a CommonJS module, the immediately preceding comment re "When 'use strict'; is used, then 'this' will be undefined anyways and the emit could simply be ..." is not valid under all circumstances. It is only valid in the use case whence Foo is invoked as a top-level function (either via new or normal function call) in the same source file. When otherwise invoked as an imported function/constructor combo, it depends on whether the reference to Foo is obtained from a top-level variable (case 1) or via an import member in which case the reference is possibly, depending on codegen, obtained by a property access (case 2), i.e.
For the moment, I'm going with this method in my code albeit correct though not completely generic (since have to type === functionname).
|
There isn't a lot in the current ES6 spec draft, but it seems to indicate the constructor object that is in the process of being
new
'd at runtime.The text was updated successfully, but these errors were encountered: