ES6箭头函数和Function.prototype.bind绑定的函数之间有什么区别(如果有)? [英] What are the differences (if any) between ES6 arrow functions and functions bound with Function.prototype.bind?

查看:191
本文介绍了ES6箭头函数和Function.prototype.bind绑定的函数之间有什么区别(如果有)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,在ES6中,以下两个函数很相似:

It seems to me that, in ES6, the following two functions are very nearly identical:

function () {
  return this;
}.bind(this);

() => {
  return this;
};

最终结果似乎相同:arrow函数产生一个JavaScript函数对象,其这个上下文绑定到与这个创建它们相同的值。

The end result seems the same: arrow functions produce a JavaScript function object with their this context bound to the same value as the this where they are created.

显然,在一般意义上, Function.prototype.bind 比arrow函数更灵活:它可以绑定到本地以外的值,它可以在任何时间点绑定任何函数的,这可能在最初创建之后很长时间。但是,我不是问如何 bind 本身不同于箭头函数,我问如何使用箭头函数与立即调用 bind with this

Obviously, in the general sense, Function.prototype.bind is more flexible than arrow functions: it can bind to values other than the local this, and it can bind any function's this at any point in time, potentially long after it is initially created. However, I'm not asking how bind itself is different from arrow functions, I'm asking how arrow functions differ from immediately calling bind with this.

ES6中的两个结构之间是否有区别?

Are there any differences between the two constructs in ES6?

推荐答案

没有(重大)差异。



嗯,小过早

There are no (significant) differences.

Well, okay, that's a little premature. There are three tiny differences unique to arrow functions.


  1. 箭头功能不能与 new

这意味着,他们当然没有一个原型属性,不能用于创建具有经典启发语法的对象。

This means, of course, that they do not have a prototype property and cannot be used to create an object with the classically-inspired syntax.

new (() => {}) // TypeError: () => {} is not a constructor

尽管如此,这可能是最好的方式新的作品对于绑定的功能将不会很有意义。

This is probably for the best, though—the way new works would not make much sense with bound functions.

箭头功能无法访问特殊的参数普通JavaScript函数可以访问的对象。

Arrow functions do not have access to the special arguments object that ordinary JavaScript functions have access to.

(() => arguments)(1, 2, 3) // ReferenceError: arguments is not defined

一点点更多的一个骗子。大概这是为了删除JavaScript的其他怪物之一。 参数对象是它自己的特殊的野兽,它有奇怪的行为,所以并不奇怪,它被抛弃。

This one is probably a little bit more of a gotcha. Presumably this is to remove one of JavaScript's other oddities. The arguments object is its own special beast, and it has strange behavior, so it's not surprising that it was tossed.

相反,ES6有可以完成相同的事情,没有任何魔术隐藏的变量:

Instead, ES6 has splats that can accomplish the same thing without any magic hidden variables:

((...args) => args)(1, 2, 3) // [1, 2, 3]


  • 箭头函数没有他们自己的 new.target 属性,他们使用 new.target 其包含的功能,如果它存在。

  • Arrow functions do not have their own new.target property, they use the new.target of their enclosing function, if it exists.

    这与删除神奇的其他更改一致箭头功能的值。如上所述,考虑到箭头功能不能与 new 一起使用,这一特别的变化是特别明显的。

    This is consistent with the other changes to remove "magically" introduced values for arrow functions. This particular change is especially obvious, considering arrow functions can't be used with new anyway, as mentioned above.



      否则,箭头就像绑定的函数,在语义上。由于箭头不必携带附加行李,因此不需要首先从普通功能转换,而是在行为上完全一样。

    Otherwise, arrows are just like bound functions, semantically. It's possible for arrows to be more performant, since they don't have to carry around the extra baggage and since they don't need to be converted from ordinary functions first, but they're behaviorally exactly the same.

    这篇关于ES6箭头函数和Function.prototype.bind绑定的函数之间有什么区别(如果有)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆