如何正确地从设计器的一个功能设置属性到另一个函数?



我想设置weapon属性bow给弓箭手。但是,当您调用toString方法时,将显示一个错误:

this.weapon._getDamage不是函数

function Weapon(name, damage) {
this.name = name;
this.damage = damage;
Object.defineProperty(this, 'toString', {
value: function () {
return name + ' damage: ' + damage + " points";
}
});
}
var bow = new Weapon('Golden bow, ', 20);
console.log(bow.toString()); // Golden bow,  damage: 20 points
function Unit(basicDamage, type) {
this._basicDamage = basicDamage;
this._type = type;
this._setWeapon = function (weapon) {
var me = this;
me.weapon = weapon;
return me;
};
this._getDamage = function () {
return (this.weapon ? this.weapon._getDamage() : 0) + this._basicDamage;
};
this.toString = function () {
return "Type-" + this._type + ",Have weapon -" + this.weapon +
"," +  ', hero current damage-' + this._getDamage() + 'points';
}
}
function Archer(basicDamage) {
Unit.apply(this, arguments);
this._type = "archer";
}
var archer = new Archer(50);
archer._setWeapon(bow);
console.log(archer.toString());

你必须把它转换成数字

return +this.weapon ? this.weapon._getDamage() : 0 + this._basicDamage;

function Weapon(name, damage) {
this.name = name;
this.damage = damage;
Object.defineProperty(this, 'toString', {
value: function () {
return name + ' damage: ' + damage + " points";
}
});
}
var bow = new Weapon('Golden bow, ', 20);
function Unit(basicDamage, type) {
this._basicDamage = basicDamage;
this._type = type;
this._setWeapon = function (weapon) {
var me = this;
me.weapon = weapon;
return me;
};
this._getDamage = function () {
return +this.weapon ? this.weapon._getDamage() : 0 + this._basicDamage;
};
this.toString = function () {
return "Type-" + this._type + ",Have weapon -" + this.weapon +"," +  ', hero current damage-' + this._getDamage() + 'points';
}
}
function Archer(basicDamage) {
Unit.apply(this, arguments);
this._type = "archer";
}
var archer = new Archer(50);
archer._setWeapon(bow);
console.log(archer.toString());

武器具有damage属性,而不是getDamage()getDamage()是你正在建立的方法。

此外,您在获取namedamage属性时忘记了Weapon.toString()中的this

您的setWeapon资源不需要返还me(或与此相关的任何内容(。您需要做的就是设置属性值。

接下来,我没有看到Archer构造函数的意义。由于Unit无论如何都希望有一个type论点,那就new Unit()吧.

最后,方法应该添加到构造函数的prototype,而不是构造函数本身。这允许该方法仅定义和存储一次,而不是在每个实例上。除此之外,您实际上不需要Object.defineProperty(),因为您只需在prototype上创建新属性即可。

function Weapon(name, damage) {
this.name = name;
this.damage = damage;
}
// Methods should be added to the prototype, not the constructor function
Weapon.prototype.toString =  function () {
return this.name + ' damage: ' + this.damage + " points";
};
function Unit(basicDamage, type) {
this._basicDamage = basicDamage;
this._type = type;
}
Unit.prototype._setWeapon = function (weapon) {
this.weapon = weapon;
};
Unit.prototype._getDamage = function () {
// Weapons have "damage", not "getDamage()"
return (this.weapon ? this.weapon.damage : 0) + this._basicDamage;
};
Unit.prototype.toString = function () {
return "Type-" + this._type + ", Have weapon -" + this.weapon +
"," +  ', hero current damage-' + this._getDamage() + 'points';
};
var bow = new Weapon('Golden bow,', 20);
console.log(bow.toString()); // Golden bow,  damage: 20 points
var archer = new Unit(50, "Archer");
archer._setWeapon(bow);
console.log(archer.toString());

最新更新