"“;变量未引用正确的对象
"that" variable not referring to correct object
当我对对象使用方法时,我的'this'变量没有引用正确的对象。
例如,isaac.healthItem.use()在控制台中显示"Mia",而不是"isaac"。
为什么"this"不是指调用对象?
var get = function(id) {
return document.getElementById(id);
}
var that;
var divider = "'n-----------------------";
var Adventurer = function(name, weapon, health, mana) {
this.name = name;
this.health = health;
this.maxHealth = health;
this.maxOverheal = this.maxHealth * 2;
this.mana = mana;
this.inventory = [];
this.spells = [];
this.weapon = weapon;
that = this;
}
Adventurer.prototype.addToInventory = function(item) {
this.inventory.push(item);
}
Adventurer.prototype.addSpell = function(spell) {
this.spells.push(spell);
}
Adventurer.prototype.setWeapon = function(weapon) {
this.weapon = weapon;
}
var HealthItem = function() {
this.healthItemName = "";
this.healAmount = 0;
this.use = function() {
console.log(that.name);
for (var x = 0; x < that.inventory.length; x++) {
if (that.inventory[x] == "HP Pot") {
this.healthItemName = "HP Pot";
this.healAmount = 30;
that.health += this.healAmount;
console.log(that.name + " Has Used " + this.healthItemName + " For " + this.healAmount + divider);
if (that.health > that.maxHealth && that.health < that.maxOverheal) {
var overheal = that.health - that.maxHealth;
console.log("Overhealed by " + overheal + "!");
} else if (that.health > that.maxOverheal) {
console.log(that.name + " cannot " + "be " + "overhealed " + "anymore!");
that.health = that.maxOverheal;
}
that.inventory[x] = "";
return;
}
}
console.log("No Health Items in Inventory" + divider);
}
}
Adventurer.prototype.healthItem = new HealthItem();
Adventurer.prototype.adventurerData = function() {
console.log(this.name + divider);
console.log("Health: " + this.health + divider);
console.log("Mana: " + this.mana + divider);
}
Adventurer.prototype.viewSpells = function() {
for (var i = 0; i < this.spells.length; i++) {
console.log(this.spells[i] + divider);
}
}
Adventurer.prototype.useSpell = function(spell) {
for (var i = 0; i < this.spells.length; i++) {
if (this.spells[i] == spell) {
console.log(this.name + " Used " + this.spells[i] + " For" + " 30 Damage!" + divider);
return;
}
}
console.log("You don't know how to do that..." + divider);
}
Adventurer.prototype.viewInventory = function() {
for (var x = 0; x < this.inventory.length; x++) {
console.log(this.inventory[x] + divider);
}
if (this.inventory.length == 0) {
console.log("Your bag is empty");
}
}
Adventurer.prototype.lotOfPots = function() {
for (var i = 0; i < 50; i++) {
this.addToInventory("HP Pot");
}
}
var isaac = new Adventurer("Isaac", "Ragnarok", 100, 50);
var mia = new Adventurer("Mia", "Celestial Staff of Water", 80, 90);
isaac.addSpell("Earthquake");
isaac.addSpell("Push");
isaac.addSpell("Healing Wave");
mia.addSpell("Soothing Waters");
mia.addSpell("Sleet");
mia.addSpell("Waterfall");
mia.adventurerData();
问题是
var that; // Global variable
var Adventurer = function(name, weapon, health, mana) {
....
that = this; // Assign the calling object to global variable
}
var isaac = new Adventurer("Isaac", "Ragnarok", 100, 50);
var mia = new Adventurer("Mia", "Celestial Staff of Water", 80, 90);
所以that
被isaac
覆盖,然后被mia
覆盖,所以当HealthItem
调用console.log(that.name)
时,that
引用mia
这是因为"that"是一个全局变量。当你创建第二个冒险家时,你就是在用第二个冒险者的值覆盖"那个"。
您是否尝试将"that"作为参数传递给Adventurer函数?
相关文章:
- 引用对象中的通用值
- 使用连字符引用对象的属性名称
- 这在 Object 方法中不引用对象,而是引用函数绑定到的标记
- Javascript对象文字,如何使用“this”来引用对象中的变量
- AngularJS select with ng选项不更新父作用域中的引用对象属性
- 如何在ES6中使用Arrow函数引用对象内部的其他函数
- 调用时可以省略引用对象
- 从函数中引用对象时遇到问题 - Adobe Animate canvas
- 在挖空中引用对象构造函数外部视图模型的属性
- 在 JavaScript 中的回调函数中引用对象
- 从充当事件处理程序的多个方法引用对象属性
- 如何引用对象中的其他节点
- 使用 javascript 通过 ID 正确引用对象
- 使用等效的 Object.indexOf 引用对象中的对象的索引
- Javascript - 事件方法中的引用对象
- 如何避免“this”引用 DOM 元素,并引用对象
- 如何按索引引用对象属性
- 在 AngularJs 中引用对象中的数据
- 如何在没有元素的情况下获取文件引用对象
- 为什么在 JavaScript 中引用对象的不存在属性不会返回引用错误