my question is probably identical and have same motive as this here i ain't using jQuery. I would like a javascript solution.
my object looks like below:
function Person(name, age, weight) {
this._name = name;
this._weight = weight;
this._age = age;
this.A开发者_运维问答natomy = {
Weight: this._weight,
Height: function () {
//calculate height from age and weight
return this._age * this._weight;
//yeah this is stupid calculation but just a demonstration
//not to be intended and here this return the Anatomy object
//but i was expecting Person Object. Could someone correct the
//code. btw i don't like creating instance and referencing it
//globally like in the linked post
}
}
}
this.Anatomy = {
//'this' here will point to Person
this.f = function() {
// 'this' here will point to Anatomy.
}
}
Inside functions this
generally points at the next thing up a level. The most consistent way to solve this would be
this.Anatomy = {
_person: this,
Weight: this._weight,
Height: function () {
//calculate height from age and weight
return _person._age * _person._weight;
}
}
Alternatively you can do this
function Person(name, age, weight) {
this.Anatomy = {
weight: weight,
height: function() { return age*weight; }
};
}
精彩评论