迷惑javascript的构造函数和原型?

sim*_* xu 6 javascript constructor prototype-programming

function MyObject(){}
Array.prototype={};
MyObject.prototype={};
var a=new Array();
var b=new MyObject();
alert(a.constructor==Array);//true
alert(b.constructor==MyObject);//false
Run Code Online (Sandbox Code Playgroud)

use*_*716 9

Array.prototype 是不可写的财产.

因此,您的任务:

Array.prototype = {}
Run Code Online (Sandbox Code Playgroud)

......没有成功,所以它的.constructor财产没有改变.

15.4.3.1 Array.prototype

初始值Array.prototype是Array原型对象(15.4.4).

此属性具有属性 { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }

...而使用自定义构造函数,您可以分配不同的原型对象,因此您已经覆盖了引用构造函数的原始对象.constructor.