You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
However, at least in Chrome and Firefox a function's name property is readonly for ordinary functions causing the assignment of the static function name to be ignored / causing a TypeError in strict mode.
The TS example above is also valid ES 2015 so I've tested it without transpilation in a current version of Chromium (v51.0.2704.79). Interestingly, while we can not change the name property of ordinary functions in Chromium we can still do the following for the constructor function once a static name property is present:
So the name property of the constructor function is no longer read-only. Based on this I assume Chromium internally does something like
Expected behavior:
varFoo=(function(){functionFoo(){}Object.defineProperty(Foo,"name",{writable: true;});// <= required for static class property *name*Foo.name=function(){return"Hello World";};returnFoo;}());
So a static class property name requires the name property to be made writable: true first. Unfortunately, Object.defineProperty is only available since ES 5 so static properties could become problematic for a compiler target of es3.
Therefore you might also want to note, that section 9.11.2 of ES2015 spec states that the ES interpreter must not set the name property, if a function does have an OwnProperty name. So the spec does seem to take custom values for Function.name into account which is why I consider a default configuration of writable: false for Function.name being problematic. It would certainly be better if browser vendors could agree here on making Function.name writable: true by default.
The text was updated successfully, but these errors were encountered:
in the past with older versions of Chrome, due to older Chromes having set configurability of Function.name to false. See comment of @alexeagle and @rbuckton regarding issue #2836.
TypeScript Version: 1.8.10
Consider the following class definition with a static property name:
The TS emitter output for this is:
However, at least in Chrome and Firefox a function's name property is readonly for ordinary functions causing the assignment of the static function name to be ignored / causing a TypeError in strict mode.
The TS example above is also valid ES 2015 so I've tested it without transpilation in a current version of Chromium (v51.0.2704.79). Interestingly, while we can not change the name property of ordinary functions in Chromium we can still do the following for the constructor function once a static name property is present:
So the name property of the constructor function is no longer read-only. Based on this I assume Chromium internally does something like
Expected behavior:
So a static class property name requires the name property to be made
writable: true
first. Unfortunately,Object.defineProperty
is only available since ES 5 so static properties could become problematic for a compiler target ofes3
.Therefore you might also want to note, that section 9.11.2 of ES2015 spec states that the ES interpreter must not set the name property, if a function does have an OwnProperty name. So the spec does seem to take custom values for Function.name into account which is why I consider a default configuration of
writable: false
for Function.name being problematic. It would certainly be better if browser vendors could agree here on making Function.namewritable: true
by default.The text was updated successfully, but these errors were encountered: