0

I need to access properties and methods one level up in the prototype chain, when using prototypal inheritance.

Is this an acceptable technique?

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
4

1 回答 1

1

好像

Object.getPrototypeOf;

是答案。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getPrototypeOf http://ejohn.org/blog/objectgetprototypeof/

于 2013-12-15T23:40:54.910 回答