Why does my arrow function have a prototype property?

眉间皱痕 提交于 2019-12-23 12:47:21

问题


As mention in document https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions

Arrow functions do not have prototype property

but when I run this on fiddle, why does it gives an object? http://es6console.com/iwyii5vm/

Why it is giving a object?

var Foo = () => {};
console.log(Foo.prototype); 

回答1:


If you run this code in a native ES6 engine, there will not be a prototype property for arrow functions.

Example of native ES6:

var Foo = () => {};
console.log(Foo.prototype); 

However, if the code is being transpiled to ES5 code, it will not be a true arrow function, and it will have a prototype property.

Example of ES6 being transpiled with Babel:

(Babel is enabled for this snippet)

var Foo = () => {};
console.log(Foo.prototype);

In the case of es6console.com, a transpiler is being used, which is why you are seeing this behavior.




回答2:


This seems to be an implementation detail of the way es6console implements es6 features. It works correctly in Chrome, which natively supports arrow functions.



来源:https://stackoverflow.com/questions/41255943/why-does-my-arrow-function-have-a-prototype-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!