var foo = function(){
this.name = "123";
this.length = 20;
this.age = 18;
}
var bar = Object.create(foo);
bar.__proto__();
console.log(bar.name); //foo
console.log(bar.length); //0
console.log(bar.age); //18
如上所示代码,根据我的理解,bar.name应该为'123',bar.length应该为20,但是这里只有age的值。
大概理解是name,length属性和Function对象中的name,length冲突,如图:

但是不理解其中的原理和机制,求大神解答。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
你这里用Object.create(foo)创造的bar是以foo为原型的,那么你继承的length属性来自foo,也必然是writable: false的,所以如果你不对它进行配置,直接修改length肯定是不行的。
length是改不了的。类似的还有

@wangfff

这是chrome控制台的截图: