function MyObject(){}
Array.prototype={};
MyObject.prototype={};
var a=new Array();
var b=new MyObject();
alert(a.constructor==Array);//true
alert(b.constructor==MyObject);//false
function MyObject(){}
Array.prototype={};
MyObject.prototype={};
var a=new Array();
var b=new MyObject();
alert(a.constructor==Array);//true
alert(b.constructor==MyObject);//false
Array.prototype
是一个不可写的属性。
因此,你的赋值操作:
Array.prototype = {}
...由于它没有成功,因此其 .constructor
属性未更改。
Array.prototype
的初始值是 Array 原型对象 (15.4.4)。此属性具有以下特性:
{ [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }
...而对于您的自定义构造函数,您可以分配不同的原型对象,因此您已经覆盖了原始对象,该对象通过 .constructor
引用构造函数。
当你用自己的空对象实例覆盖prototype
属性时,constructor
属性会被重写,因为({}).constructor === Object
。你可以选择以下任一方式:
function MyObject() {}
MyObject.prototype = {};
MyObject.prototype.constructor = MyObject;
或者(在我看来更好的方法)你可以不直接设置prototype
,而是进行扩展:
function MyObject() {}
MyObject.prototype.foo = "bar";
Array.prototype
不可写,因此你的代码 Array.prototype = {}
无声无息地失败(或在严格模式下会有提示)。> function MyObject(){}
> Array.prototype={};
你不能给Array.prototype赋值。
> MyObject.prototype={};
> var a=new Array();
> var b=new MyObject();
> alert(a.constructor==Array);//true
Array.prototype 拥有一个 constructor 属性,用于引用 Array 函数。由于 a 是 Array 的一个实例,它继承了 Array.prototype 的 constructor 属性。
> alert(b.constructor==MyObject);//false
你已经将一个空对象分配给了MyObject.prototype,它没有prototype属性,也没有b。
MyObject.prototype.constructor = MyObject;
alert(b.constructor==MyObject); // true