更改 [].__proto__.constructor 和 [].constructor 的行为不同
behaviour of changing [].__proto__.constructor and [].constructor differs
我目前正在尝试理解 javascript 中的 constructor
属性。
请注意,我知道应该避免更改内置的属性,我正在尝试它是因为我想更好地理解基本原理。
我试图更改 []
的默认 constructor
属性(即数组对象的默认构造函数)
[].__proto__.constructor === [].constructor; // true
[].constructor = function A(){}; // attempts to reset the constructor property to a new function
[].constructor; // prints ƒ Array() { [native code] }, which indicate the attempt failed
但是当我检查 [].constructor
的 属性 描述符时
Object.getOwnPropertyDescriptor([].__proto__, 'constructor');
打印
{value: ƒ, writable: true, enumerable: false, configurable: true}
所以 [].__proto__.constructor
属性 是 writable
?
所以我尝试通过[].__proto__
设置constructor
属性,它成功了
[].__proto__.constructor = function B(){};
[].__proto__.constructor; // prints: ƒ B(){}, which indicate the attempt succeded
为什么通过 []
更改 constructor
属性 失败但通过 [].__proto__
成功?即使 [].constructor === [].__proto__.constructor
返回 true
。
这是由于 属性 原型链上的阴影。当你执行
[].constructor = ...;
这会在 shadows class 原型构造函数的数组上创建一个实例 属性。但是,由于 Array.prototype
已经有自己的 constructor
属性,执行
[].__proto__.constructor = ...;
覆盖 Array.prototype
上的构造函数。
您可以通过实际存储数组实例并仔细查看其原型链来确认此行为:
下面验证分配实际上在 array1
上创建了自己的 属性,它隐藏了从 Array.prototype
.
继承的 属性
function A(){}
var array1 = [];
array1.constructor = A;
console.log(array1.constructor === A);
console.log(array1.__proto__.constructor === Array);
.__proto__
是单变量。当您在其修改的所有实例上修改它时。 __proto__
两个不同的数组是一样的。
console.log([].__proto__ === [1,23,3123].__proto__) //true
但是当你改变数组 []
实例的 constructor
属性 时。它不会更改所有实例中的所有 __proto__
。
console.log([] === []) //false
但是,如果您将数组存储在变量中,然后更改它 属性,它将起作用。
let arr = [];
arr.constructor = function A(){}
console.log(arr.constructor)
我目前正在尝试理解 javascript 中的 constructor
属性。
请注意,我知道应该避免更改内置的属性,我正在尝试它是因为我想更好地理解基本原理。
我试图更改 []
的默认 constructor
属性(即数组对象的默认构造函数)
[].__proto__.constructor === [].constructor; // true
[].constructor = function A(){}; // attempts to reset the constructor property to a new function
[].constructor; // prints ƒ Array() { [native code] }, which indicate the attempt failed
但是当我检查 [].constructor
Object.getOwnPropertyDescriptor([].__proto__, 'constructor');
打印
{value: ƒ, writable: true, enumerable: false, configurable: true}
所以 [].__proto__.constructor
属性 是 writable
?
所以我尝试通过[].__proto__
设置constructor
属性,它成功了
[].__proto__.constructor = function B(){};
[].__proto__.constructor; // prints: ƒ B(){}, which indicate the attempt succeded
为什么通过 []
更改 constructor
属性 失败但通过 [].__proto__
成功?即使 [].constructor === [].__proto__.constructor
返回 true
。
这是由于 属性 原型链上的阴影。当你执行
[].constructor = ...;
这会在 shadows class 原型构造函数的数组上创建一个实例 属性。但是,由于 Array.prototype
已经有自己的 constructor
属性,执行
[].__proto__.constructor = ...;
覆盖 Array.prototype
上的构造函数。
您可以通过实际存储数组实例并仔细查看其原型链来确认此行为:
下面验证分配实际上在 array1
上创建了自己的 属性,它隐藏了从 Array.prototype
.
function A(){}
var array1 = [];
array1.constructor = A;
console.log(array1.constructor === A);
console.log(array1.__proto__.constructor === Array);
.__proto__
是单变量。当您在其修改的所有实例上修改它时。 __proto__
两个不同的数组是一样的。
console.log([].__proto__ === [1,23,3123].__proto__) //true
但是当你改变数组 []
实例的 constructor
属性 时。它不会更改所有实例中的所有 __proto__
。
console.log([] === []) //false
但是,如果您将数组存储在变量中,然后更改它 属性,它将起作用。
let arr = [];
arr.constructor = function A(){}
console.log(arr.constructor)