【发布时间】:2011-05-27 06:09:10
【问题描述】:
这里是代码
function Person(name, age, weight) {
this._name = name;
this._weight = weight;
this._age = age;
}
Person.prototype = {
Anatomy: {
Weight: this._weight,
Height: (function () {
//calculate height from age and weight
})
}
}
当我运行此代码时,我预计 Anatomy.weight 为 60:
var x = new Person('jack',24,60);
console.dir(x.Anatomy);
令我惊讶的是它是未定义的。经检查,this 似乎指的是全局对象窗口。现在这里发生了什么:(
我希望this._weight 引用 Person 对象的重量,否则从粗略计算来看,这至少应该引用 Anatomy,因为它是一个对象。有人可以澄清疑问
【问题讨论】:
标签: javascript object window this prototype-programming