[英]JavaScript Prototypal Inheritance Chain: “that” technique for accessing upper level properties and methods?
使用原型继承时,我需要访问原型链中上一级的属性和方法。
这是可以接受的技术吗?
function Cheese() {
this.weight = 100;
this.unit = 'g';
this.that = this; // not sure about this... does it create a circular reference?
}
Cheese.prototype.setWeight = function(myWeight) {
this.weight = myWeight;
}
var cheese = new Cheese();
var myCheddar = Object.create(cheese); // http://javascript.crockford.com/prototypal.html
myCheddar.setWeight(500);
console.log(myCheddar.weight + myCheddar.unit); //-> 100kg
console.log(myCheddar.that.weight + myCheddar.unit); //-> 500g
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.