Javascript 正在努力使ES6此绑定正常工作
我写了这段代码,试图创建一个即兴格斗游戏。它使用旧式的function关键字,因此当我想使用一个helper函数isAlive时,我不得不执行一个非常难看的“this”绑定 我试图理解“this”与arrow函数的不同行为,但当我将第一行更改为ES6语法并删除绑定时,它总是返回falseJavascript 正在努力使ES6此绑定正常工作,javascript,ecmascript-6,this,bind,arrow-functions,Javascript,Ecmascript 6,This,Bind,Arrow Functions,我写了这段代码,试图创建一个即兴格斗游戏。它使用旧式的function关键字,因此当我想使用一个helper函数isAlive时,我不得不执行一个非常难看的“this”绑定 我试图理解“this”与arrow函数的不同行为,但当我将第一行更改为ES6语法并删除绑定时,它总是返回false let isAlive = () => this.health > 0; 我做错了什么?我很确定语法本身是正确的 let isAlive = function () { return this.h
let isAlive = () => this.health > 0;
我做错了什么?我很确定语法本身是正确的
let isAlive = function () { return this.health > 0};
let getFighter = function (color, strength) {
return {
color: color,
health: 100,
strength: typeof strength !== 'undefined' ? strength : 1,
attack: function (what) {
hit_points = Math.floor(Math.random() * 10 * this.strength);
document.writeln(this.color + ' fighter attacks ' + what.color + ' trying to deal ' + hit_points + ' damage.');
what.get_hurt(hit_points);
},
get_hurt: function (hit_points) {
if ( !isAlive.bind(this)() ) {
document.writeln(this.color + ' fighter already dead!');
document.writeln();
return;
}
this.health = this.health - hit_points;
document.writeln(this.color + ' received ' + hit_points + ' damage and has ' + this.health + ' HP left.');
if ( !isAlive.bind(this)() ) {
document.writeln(this.color + ' fighter died!');
}
document.writeln();
}
};
};
blue = getFighter('Blue', 3);
red = getFighter('Red');
console.log(red);
console.log(blue);
while (isAlive.bind(blue)()) {
red.attack(blue);
}
red.attack(blue)
看看您的代码示例,我认为我们首先需要了解
这个
是如何工作的<代码>此是指已实例化的对象/函数
const getFighter = function(color, strength) {
this.color = color;
this.strength = strength && 1;
return {
health: 100,
isAlive: this.health > 0,
hit_points: () => Math.floor(Math.random() * 10 * this.strength),
attack: (what) => {
document.writeln(`${this.color} fighter attacks ${what.color} trying to deal ${this.hit_points} damage.`);
what.get_hurt(this.hit_points);
return null;
},
get_hurt: () => {
if (!this.isAlive) {
document.writeln(`${this.color} fighter already dead!`);
return;
}
this.health = this.health - this.hit_points;
document.writeln(`${this.color} received ${this.hit_points} damage and has ${this.health} HP left.`);
if (!this.isAlive) {
document.writeln(`${this.color} fighter died!`);
}
},
};
};
const BlueFighter = new getFighter("Blue", 3);
const RedFighter = new getFighter("Red");
console.log('is Alive', BlueFighter.isAlive);
console.log('Health:', RedFighter.health);
您可以看到我利用了()=>
,这样我就可以在函数中访问此
。除非您.bind(this)
,否则无法使用常规函数获得此值 箭头函数不创建自己的this,而是使用封闭执行上下文的this值
通过在不同的执行上下文中定义isAlive,还可以将关键字绑定到不同的this
如果要利用箭头函数,请在函数中声明它们
function foo(){
const isAlive = () => this.health > 100;
}
在您的情况下,如果您想要一个助手,您可以将其声明为对象的一部分,或者使用ES6类,类属性将为您实现这一点。您可以在
isAlive
函数中定义一个参数,并将该对象或此
传递给该函数
let isAlive=({health})=>health>0;
让getFighter=功能(颜色、强度){
返回{
颜色:颜色,
健康:100,
强度:强度类型!=“未定义”?强度:1,
攻击:功能(什么){
hit_points=Math.floor(Math.random()*10*this.strength);
document.writeln(this.color+“fighter attacks”+what.color
+“试图造成”+生命值+伤害“);
什么。受伤(命中率);
},
受伤:功能(生命值){
如果(!isAlive(this)){
document.writeln(this.color+'fighter ready dead!');
document.writeln();
返回;
}
this.health=this.health-生命值;
document.writeln(this.color+“已接收”+命中率
+'伤害并有'+this.health+'生命剩余');
如果(!isAlive(this)){
document.writeln(this.color+'fighter dead!');
}
document.writeln();
}
};
};
蓝色=getFighter('blue',3);
红色=getFighter(“红色”);
控制台日志(红色);
控制台日志(蓝色);
while(我还活着(蓝色)){
红色。攻击(蓝色);
}
红色。攻击(蓝色);
控制台日志(红色);
控制台日志(蓝色)
如果isAlive
函数不使用正常的函数
语法。如果你那样做的话,我想应该行得通。内部的this
称为词法this
。请看一看以获得解释。isAlive
为什么在您正在检查的对象之外?如果通过检查typeof(this.health)
未定义this.health
,则该语句将始终为false。听起来你想把它放在与攻击
、受伤
等相同的对象中。目前只有一个战士对象,但我想添加不同的对象,并对它们应用isAlive()。不管怎么说,这只是教育性的,我的主要目标是实际使用该语言进行实验。只有在使用新关键字创建对象时,这才有效。