ES6 对象中的方法:使用箭头函数
Methods in ES6 objects: using arrow functions
在 ES6 中,这两个都是合法的:
var chopper = {
owner: 'Zed',
getOwner: function() { return this.owner; }
};
并且,作为 shorthand:
var chopper = {
owner: 'Zed',
getOwner() { return this.owner; }
}
是否也可以使用新的箭头函数?在尝试类似
var chopper = {
owner: 'John',
getOwner: () => { return this.owner; }
};
或
var chopper = {
owner: 'John',
getOwner: () => (this.owner)
};
我收到一条错误消息,提示该方法无法访问 this
。这只是一个语法问题,还是你不能在 ES6 对象中使用 fat-arrow 方法?
这一行 getOwner: () => this.owner
应该是:
var chopper = {
owner: 'John',
getOwner: () => this.owner
}; //here `this` refers to `window` object.
console.log(chopper.getOwner());
您必须将 this
声明为一个函数:
var chopper = {
owner: 'John',
getOwner() { return this.owner }
};
console.log(chopper.getOwner());
或:
var chopperFn = function(){
this.setOwner = (name) => this.owner = name;
Object.assign(this,{
owner: 'Jhon',
getOwner: () => this.owner,
})
}
var chopper = new chopperFn();
console.log(chopper.getOwner());
chopper.setOwner('Spiderman');
console.log(chopper.getOwner());
箭头函数并非设计用于所有情况,而只是作为老式函数的简化版本。它们并非旨在使用 function
关键字替换函数语法。箭头函数最常见的用例是短 "lambdas",它不会重新定义 this
,通常在将函数作为回调传递给某个函数时使用。
箭头函数不能用于编写对象方法,因为正如您所发现的,由于箭头函数关闭词法封闭上下文的 this
,因此箭头中的 this
就是那个那是您定义对象的当前位置。也就是说:
// Whatever `this` is here...
var chopper = {
owner: 'Zed',
getOwner: () => {
return this.owner; // ...is what `this` is here.
}
};
在你的例子中,想要在一个对象上写一个方法,你应该简单地使用传统的 function
语法,或者 ES6 中引入的方法语法:
var chopper = {
owner: 'Zed',
getOwner: function() {
return this.owner;
}
};
// or
var chopper = {
owner: 'Zed',
getOwner() {
return this.owner;
}
};
(它们之间存在细微差别,但只有当您在 getOwner
中使用 super
时它们才重要,而您不是,或者如果您将 getOwner
复制到另一个对象。)
在 es6 邮件列表上有一些关于箭头函数的扭曲的争论,箭头函数具有相似的语法但有自己的this
。然而,这个提议并没有被接受,因为它只是语法糖,让人们可以节省输入几个字符,并且没有提供比现有函数语法更多的新功能。见题目unbound arrow functions.
这个内部箭头函数不反映对象的上下文。相反,它给出了调用对象方法的上下文。
检查这个,这可以让您了解何时使用箭头,何时不使用。
https://dmitripavlutin.com/when-not-to-use-arrow-functions-in-javascript/
如果一定要用箭头函数,可以把this
改成chopper
,
var chopper = {
owner: "John",
getOwner: () => chopper.owner
};
虽然这不是最佳实践,但当您更改对象名称时,您必须更改此箭头功能。
我遵循的使用箭头函数的快速提示。
- 对将使用
object.method()
语法的方法使用非箭头函数。 (这些函数将从调用者那里接收到有意义的 this
值。)
- 几乎所有其他事情都使用箭头函数。
另一个提示,在严格模式下,this
仍然指的是 Window 而不是 undefined。
(() => {
"use strict";
console.log(this); // window
})();
(function () {
"use strict";
console.log(this); // undefined
})();
在 ES6 中,这两个都是合法的:
var chopper = {
owner: 'Zed',
getOwner: function() { return this.owner; }
};
并且,作为 shorthand:
var chopper = {
owner: 'Zed',
getOwner() { return this.owner; }
}
是否也可以使用新的箭头函数?在尝试类似
var chopper = {
owner: 'John',
getOwner: () => { return this.owner; }
};
或
var chopper = {
owner: 'John',
getOwner: () => (this.owner)
};
我收到一条错误消息,提示该方法无法访问 this
。这只是一个语法问题,还是你不能在 ES6 对象中使用 fat-arrow 方法?
这一行 getOwner: () => this.owner
应该是:
var chopper = {
owner: 'John',
getOwner: () => this.owner
}; //here `this` refers to `window` object.
console.log(chopper.getOwner());
您必须将 this
声明为一个函数:
var chopper = {
owner: 'John',
getOwner() { return this.owner }
};
console.log(chopper.getOwner());
或:
var chopperFn = function(){
this.setOwner = (name) => this.owner = name;
Object.assign(this,{
owner: 'Jhon',
getOwner: () => this.owner,
})
}
var chopper = new chopperFn();
console.log(chopper.getOwner());
chopper.setOwner('Spiderman');
console.log(chopper.getOwner());
箭头函数并非设计用于所有情况,而只是作为老式函数的简化版本。它们并非旨在使用 function
关键字替换函数语法。箭头函数最常见的用例是短 "lambdas",它不会重新定义 this
,通常在将函数作为回调传递给某个函数时使用。
箭头函数不能用于编写对象方法,因为正如您所发现的,由于箭头函数关闭词法封闭上下文的 this
,因此箭头中的 this
就是那个那是您定义对象的当前位置。也就是说:
// Whatever `this` is here...
var chopper = {
owner: 'Zed',
getOwner: () => {
return this.owner; // ...is what `this` is here.
}
};
在你的例子中,想要在一个对象上写一个方法,你应该简单地使用传统的 function
语法,或者 ES6 中引入的方法语法:
var chopper = {
owner: 'Zed',
getOwner: function() {
return this.owner;
}
};
// or
var chopper = {
owner: 'Zed',
getOwner() {
return this.owner;
}
};
(它们之间存在细微差别,但只有当您在 getOwner
中使用 super
时它们才重要,而您不是,或者如果您将 getOwner
复制到另一个对象。)
在 es6 邮件列表上有一些关于箭头函数的扭曲的争论,箭头函数具有相似的语法但有自己的this
。然而,这个提议并没有被接受,因为它只是语法糖,让人们可以节省输入几个字符,并且没有提供比现有函数语法更多的新功能。见题目unbound arrow functions.
这个内部箭头函数不反映对象的上下文。相反,它给出了调用对象方法的上下文。
检查这个,这可以让您了解何时使用箭头,何时不使用。 https://dmitripavlutin.com/when-not-to-use-arrow-functions-in-javascript/
如果一定要用箭头函数,可以把this
改成chopper
,
var chopper = {
owner: "John",
getOwner: () => chopper.owner
};
虽然这不是最佳实践,但当您更改对象名称时,您必须更改此箭头功能。
我遵循的使用箭头函数的快速提示。
- 对将使用
object.method()
语法的方法使用非箭头函数。 (这些函数将从调用者那里接收到有意义的this
值。) - 几乎所有其他事情都使用箭头函数。
另一个提示,在严格模式下,this
仍然指的是 Window 而不是 undefined。
(() => {
"use strict";
console.log(this); // window
})();
(function () {
"use strict";
console.log(this); // undefined
})();