-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
new myVariable.constructor()
doesn't compile
#4356
Comments
The compiler will still emit code in the presence of errors unless given the |
This code should be supported on all browsers, given https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor. I appreciate the I strongly object to the |
new myVariable.constructor()
doesn't worknew myVariable.constructor()
doesn't compile
I apologize, I misread the original issue. I thought you were suggesting that the code should not have been emitted at all. |
The |
I can't find any evidence supporting this. All runtimes seem to hook this up automatically.
I can't find any evidence supporting this, either. The ES spec doesn't define that this property is present as a result of object creation. http://www.ecma-international.org/ecma-262/6.0/#sec-evaluatenew |
MDN lists the relevant specs near the bottom of the page - this is the most relevant: |
@jiaweihli but all that says is that the actual field |
@RyanCavanaugh I believe the spec is a bit unclear here - it says the field This has been the stance of MDN since the earliest version of that page, so that's most likely their interpretation of the ES1 spec. The relevant section begins on page 73 here. I don't necessarily have the knowledge needed to resolve this ambiguity, but I think it's worth looking into this since a lot of freely available documentation lists the behavior I mentioned above. Anecdotally, the code has worked for me in several different environments, and is also supported by all major browsers. |
@jiaweihli appears to be right. Given the following in Chrome 44: class A {
constructor () {
console.log('I was called');
}
}
let a = new A();
let aa = new a.constructor();
|
I get that this works in many runtimes, but I'm still not seeing it being spec'd behavior. Why does the fact that |
Correct me if I am wrong, but doesn't
And intrinsic objects are described as:
|
Pinging @bterlson for this one. |
I think you can strongly type constructor for ES6 classes. The constructor property is added to the People above are also correct that Object.prototype has a constructor property that points to Object (and thus any object with Object.prototype in its prototype chain and no other constructor properties will have a .constructor property pointing to Object). But I don't think this has any bearing on whether you can strongly type the .constructor property of class instances. |
Duplicate of #3841 |
Reopening since #3841 is for the static side whereas this is for the instance side |
Let's track at #4586 |
This code throws a compile error, but appears to produce valid code when entered into http://www.typescriptlang.org/Playground.
EDIT:
This code correctly compiles after changing the last line to:
The text was updated successfully, but these errors were encountered: