繁体   English   中英

JavaScript原型继承链:用于访问上层属性和方法的“那个”技术?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM