Fel*_*ing 449 javascript ecmascript-6 arrow-functions
规范问题如果在用箭头函数替换函数声明/表达式后发现有关问题的问题,请将其作为此副本的副本关闭.
ES2015中的箭头功能提供了更简洁的语法.我现在可以用箭头功能替换所有函数声明/表达式吗?我需要注意什么?
例子:
构造函数
function User(name) {
this.name = name;
}
// vs
const User = name => {
this.name = name;
};
Run Code Online (Sandbox Code Playgroud)
原型方法
User.prototype.getName = function() {
return this.name;
};
// vs
User.prototype.getName = () => this.name;
Run Code Online (Sandbox Code Playgroud)
对象(文字)方法
const obj = {
getName: function() {
// ...
}
};
// vs
const obj = {
getName: () => {
// ...
}
};
Run Code Online (Sandbox Code Playgroud)
回调
setTimeout(function() {
// ...
}, 500);
// vs
setTimeout(() => {
// ...
}, 500);
Run Code Online (Sandbox Code Playgroud)
变量函数
function sum() {
let args = [].slice.call(arguments);
// ...
}
// vs
const sum = (...args) => {
// ...
};
Run Code Online (Sandbox Code Playgroud)
Fel*_*ing 665
tl;博士: 不!箭头函数和函数声明/表达式不是等价的,不能盲目替换.
如果要替换的函数不使用this
,arguments
并且未调用new
,则为yes.
经常这样:这取决于.箭头函数与函数声明/表达式具有不同的行为,因此我们先看一下差异:
词汇this
和arguments
箭头功能没有自己的this
或arguments
绑定.相反,这些标识符在词法范围内像任何其他变量一样被解析.这意味着在箭头函数内部,this
并arguments
参考环境中的值this
和arguments
箭头函数的定义(即箭头函数的"外部"):
// Example using a function expression
function createObject() {
console.log('Inside `createObject`:', this.foo);
return {
foo: 42,
bar: function() {
console.log('Inside `bar`:', this.foo);
},
};
}
createObject.call({foo: 21}).bar(); // override `this` inside createObject
Run Code Online (Sandbox Code Playgroud)
// Example using a arrow function
function createObject() {
console.log('Inside `createObject`:', this.foo);
return {
foo: 42,
bar: () => console.log('Inside `bar`:', this.foo),
};
}
createObject.call({foo: 21}).bar(); // override `this` inside createObject
Run Code Online (Sandbox Code Playgroud)
在函数表达式的情况下,this
指的是在函数内部创建的对象createObject
.在箭头函数的情况下,this
指的this
是createObject
它自身.
如果您需要访问this
当前环境,这使得箭头函数很有用:
// currently common pattern
var that = this;
getData(function(data) {
that.data = data;
});
// better alternative with arrow functions
getData(data => {
this.data = data;
});
Run Code Online (Sandbox Code Playgroud)
请注意,这也意味着无法this
使用.bind
或设置箭头功能.call
.
如果您不是很熟悉this
,请考虑阅读
2.无法调用箭头功能 new
ES2015区分了可调用的函数和可构造的函数.如果函数是可构造的,则可以调用它 new
,即new User()
.如果函数是可调用的,则可以不调用它new
(即正常函数调用).
通过函数声明/表达式创建的函数既可构造又可调用.
箭头函数(和方法)只能调用.
class
构造函数只能构造.
如果您尝试调用不可调用的函数或构造不可构造的函数,则会出现运行时错误.
知道了这一点,我们可以说明以下内容.
更换:
this
或的功能arguments
..bind(this)
不可更换:
this
)arguments
(见下文))让我们用你的例子仔细研究一下:
构造函数
这不起作用,因为无法调用箭头函数new
.继续使用函数声明/表达式或使用class
.
原型方法
很可能不是,因为原型方法通常用于this
访问实例.如果他们不使用this
,那么你可以替换它.但是,如果您主要关注简洁的语法,请使用class
其简洁的方法语法:
class User {
constructor(name) {
this.name = name;
}
getName() {
return this.name;
}
}
Run Code Online (Sandbox Code Playgroud)
对象方法
对于对象文字中的方法也是如此.如果方法想要通过引用对象本身this
,继续使用函数表达式,或使用新方法语法:
const obj = {
getName() {
// ...
},
};
Run Code Online (Sandbox Code Playgroud)
回调
这取决于.如果您对外部别名this
或使用.bind(this)
以下内容,则应该更换它:
// old
setTimeout(function() {
// ...
}.bind(this), 500);
// new
setTimeout(() => {
// ...
}, 500);
Run Code Online (Sandbox Code Playgroud)
但是:如果调用回调的代码显式设置this
为特定值,通常是事件处理程序的情况,特别是使用jQuery,并且回调使用this
(或arguments
),则不能使用箭头函数!
变量函数
由于箭头功能没有自己的功能arguments
,因此您不能简单地用箭头功能替换它们.但是,ES2015引入了使用的替代方法arguments
:rest参数.
// old
function sum() {
let args = [].slice.call(arguments);
// ...
}
// new
const sum = (...args) => {
// ...
};
Run Code Online (Sandbox Code Playgroud)
相关问题:
更多资源:
小智 24
箭头函数 => 迄今为止最好的 ES6 特性。它们是 ES6 的一个非常强大的补充,我经常使用。
等等,你不能在你的代码中到处使用箭头函数,它不会在所有情况下都起作用,比如this
箭头函数不可用的地方。毫无疑问,箭头函数是一个很好的补充,它带来了代码的简单性。
但是当需要动态上下文时不能使用箭头函数:定义方法,使用构造函数创建对象,在处理事件时从中获取目标。
他们没有 this
它使用“词法范围”来确定“ ”的值this
应该是什么。在简单的词法作用域中,它使用this
函数体内的“ ”。
他们没有 arguments
箭头函数没有arguments
对象。但是使用其余参数可以实现相同的功能。
let sum = (...args) => args.reduce((x, y) => x + y, 0)
sum(3, 3, 1) // output - 7
`
它们不能与 new
箭头函数不能是构造函数,因为它们没有原型属性。
map
、reduce
、 或forEach
。 归档时间: |
|
查看次数: |
118086 次 |
最近记录: |