Question

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

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top