小编Ang*_*usC的帖子

为什么内置函数没有原型属性?

鉴于ES 5.1标准规定......

1)在http://es5.github.com/#x13.2脚下注意

NOTE A prototype property is automatically created for every function,
to allow for the possibility that the function will be used as a constructor.
Run Code Online (Sandbox Code Playgroud)

2)http://es5.github.com/#x15.3.5.2

NOTE Function objects created using Function.prototype.bind do not have
a prototype property.
Run Code Online (Sandbox Code Playgroud)

(这暗示所有其他功能都有)

...为什么内置函数不再具有原型属性?:

[].push.prototype; //undefined
Math.max.prototype; //undefined
Run Code Online (Sandbox Code Playgroud)

此外,即使为这些内置函数分配了一个原型属性,它们也不能用作构造函数:

[].push.prototype = {};
[].push.prototype; //[object Object]
new [].push(); //TypeError: function push() { [native code] } is not a constructor
Run Code Online (Sandbox Code Playgroud)

相反,从用户定义的对象中删除prototype属性仍然允许它用作构造函数,并且实际上将一个通用对象分配给生成的实例的[[prototype]]:

var A = function() {};
A.prototype = undefined;
A.prototype; //undefined …
Run Code Online (Sandbox Code Playgroud)

javascript ecmascript-5

9
推荐指数
1
解决办法
744
查看次数

标签 统计

ecmascript-5 ×1

javascript ×1