JavaScript Prototypal Inheritance Chain: "та" техника для доступа к свойствам и методам верхнего уровня?
Мне нужно получить доступ к свойствам и методам на один уровень выше в цепочке прототипов при использовании наследования прототипов.
Это приемлемая техника?
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
1 ответ
Похоже
Object.getPrototypeOf;
Это ответ.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getPrototypeOf http://ejohn.org/blog/objectgetprototypeof/