在 javascript "class" 中定义属性方法的正确模式
Proper pattern to define methods of properties in a javascript "class"
我正在尝试用 Person.energy 属性 创建一个人 class,其中包含一个 Person.energy.value 数字和一个 Person.energy.rest 函数恢复 Person.energy.value.
当我定义Person.energy时,我可以根据this.ability属性设置属性。但是当我尝试更新方法中的属性时,我失去了授予对 this.ability 的访问权限的范围。我制作了一个示例来解释我的意思...
在下面 Person.energy 的定义中,访问 this.ability 变量以设置 Person.energy.initial 和 .value 变量。这似乎工作正常,因为我可以 console.log 创建对象后的变量。但是,当我尝试访问相同的 this.ability 变量作为 Person.energy.rest 函数的一部分时,它是未定义的。这似乎是一个范围问题。
我知道我没有很好地组织我的 class 方法和属性,但我不知道正确的方法是什么。 什么是好的 javascript class 模式来定义仍然可以访问父级的同级属性的属性方法?
==代码==
function Person() {
this.init = function() {
//later: more complex var assignment
var talent = 0.5;
var skill = 0.5;
return [talent, skill];
};
[this.talent, this.skill] = this.init();
this.calculate();
return this;
}
Person.prototype.calculate = function() {
this.ability = this.talent * this.skill;
this.energy = {
initial: this.ability * 100,
value: this.ability * 100
};
this.energy.rest = function() {
console.log(this.ability, " <--- this.ability out of scope");
var amount = this.ability * Math.random();
this.value = this.value + amount;
};
};
p = new Person();
console.log(p.energy.value, " <-- calculated using this.ability");
p.energy.rest();
== Console.log ==
25 <-- calculated using this.ability
undefined <--- this.ability out of scope
Person.prototype.calculate = function() {
var oPerson = this;//will be referring to person object which will have scope across this function.
this.ability = this.talent * this.skill;
this.energy = {
initial: this.ability * 100,
value: this.ability * 100
};
this.energy.rest = function() {
console.log(oPerson.ability, " <--- this.ability out of scope");
var amount = oPerson.ability * Math.random();
this.value = this.value + amount;
};
};
我正在尝试用 Person.energy 属性 创建一个人 class,其中包含一个 Person.energy.value 数字和一个 Person.energy.rest 函数恢复 Person.energy.value.
当我定义Person.energy时,我可以根据this.ability属性设置属性。但是当我尝试更新方法中的属性时,我失去了授予对 this.ability 的访问权限的范围。我制作了一个示例来解释我的意思...
在下面 Person.energy 的定义中,访问 this.ability 变量以设置 Person.energy.initial 和 .value 变量。这似乎工作正常,因为我可以 console.log 创建对象后的变量。但是,当我尝试访问相同的 this.ability 变量作为 Person.energy.rest 函数的一部分时,它是未定义的。这似乎是一个范围问题。
我知道我没有很好地组织我的 class 方法和属性,但我不知道正确的方法是什么。 什么是好的 javascript class 模式来定义仍然可以访问父级的同级属性的属性方法?
==代码==
function Person() {
this.init = function() {
//later: more complex var assignment
var talent = 0.5;
var skill = 0.5;
return [talent, skill];
};
[this.talent, this.skill] = this.init();
this.calculate();
return this;
}
Person.prototype.calculate = function() {
this.ability = this.talent * this.skill;
this.energy = {
initial: this.ability * 100,
value: this.ability * 100
};
this.energy.rest = function() {
console.log(this.ability, " <--- this.ability out of scope");
var amount = this.ability * Math.random();
this.value = this.value + amount;
};
};
p = new Person();
console.log(p.energy.value, " <-- calculated using this.ability");
p.energy.rest();
== Console.log ==
25 <-- calculated using this.ability
undefined <--- this.ability out of scope
Person.prototype.calculate = function() {
var oPerson = this;//will be referring to person object which will have scope across this function.
this.ability = this.talent * this.skill;
this.energy = {
initial: this.ability * 100,
value: this.ability * 100
};
this.energy.rest = function() {
console.log(oPerson.ability, " <--- this.ability out of scope");
var amount = oPerson.ability * Math.random();
this.value = this.value + amount;
};
};