为什么原型中的属性值不一致
why the values of properties in the prototype are inconsistent?
我调用了console.log(Family.prototype.money)
,值是200,这证实了asset
是函数Family
的原型。但是当我调用console.log(other.money)
时,值是 1000,我之前将其分配给原型。这是怎么回事?看起来对象 other 的原型与函数 Family 的原型不同,这与我从《面向对象 Javascript 》一书中读到的内容完全矛盾。
function Family(father, mother, children){
this.father = father,
this.mother = mother,
this.children = children,
this.num = function(){
return (this.children.length+2);
}
}
Family.prototype.money = 1000; // notice!!!!
var myFamily = new Family('Hung', 'Hong', ['Phuong', 'Lien', 'Hiep']);
var other = new myFamily.constructor('Lan', 'Linh', ['Tung']);
var money = other.money;
var asset = {
money: 200,
car: 2,
house: 10
}
Family.prototype = asset;
函数的prototype
不是实例对象的实际原型。使用 new
时,prototype
对象将用作内部 [[Prototype]]
属性的模板,在某些浏览器中显示为 __proto__
。因此,在更改prototype
之前,此表达式true
:
Family.prototype === other.__proto__ // true
Family.prototype = asset;
而你敲打完之后,它false
:
Family.prototype = asset;
Family.prototype === other.__proto__ // false
相关文章:
- 当属性不一致时,如何根据属性对JS对象列表进行排序
- 对象不支持属性或方法“自动完成”
- Android和JavaScript解析API之间不一致
- Javascript的行为与PHP包含文件不一致
- 对这种与document.domain和CORS相关的不一致行为的解释是什么
- 点击链接行为不一致
- Passport.js`isAuthenticated()`不一致的行为;当它应该是真的时候是假的
- 与显示不一致
- Ext 4.2.1 模型日期解析在浏览器之间不一致
- 通过 Dropbox API 上传时的文件内容不一致
- 对象不支持属性或方法“确认”
- 对象不支持属性或方法“jscomp_symbol_iterator0”
- 下拉 Javascript 错误:对象不支持属性或方法“匹配”
- JavaScript - 对象属性不一致
- 运行时错误:对象不支持属性或方法
- Javascript 混淆了 null、instanceof 和 typeof 的语法不一致
- localeCompare 显示使用前导变音字符对单词进行排序时不一致的行为
- 为什么原型中的属性值不一致
- textarea-maxlength属性和字符串长度()不一致
- jQuery在分配“选定”属性时行为不一致