ES6 Arrow Functions.
When would you not want to use an arrow function?用ES6的箭头函数,我们可以将上面的add实现成这样:.
With ES6 arrow functions, we can transform the expression above into:.
There is an important difference between regular functions and arrow functions.Combinations with other parts of speech
箭头函数(匿名函数的简写)以及词法this(包含作用域this).
Arrow functions(shorter anonymous functions) and lexical this(enclosing scope this).不同于一般的函数,箭头函数不具有其自己的this,它需要从被定义的外部函数得到this。
Unlike a normal function, an arrow function does not have its own this, it takes this from the outer function where it is defined.请注意:箭头函数和函数声明/表达式不是等效的,不能盲目替换。
Be aware: Arrow functions and function declarations/ expressions are not equivalent and cannot be replaced blindly.还是让我们来看一下使用箭头函数带来的下一个优点:“隐式返回”。
Let's look at the next benefit of arrow functions,“implicit returns”.和普通函数不同,箭头函数没有它自己的this值,它的this值继承自外围作用域。
Unlike a normal function, an arrow function does not have its own this, it takes this from the outer function where it is defined.那么调用箭头函数后,this指向window,因为它从bar继承了this。
Then calling the arrow function this is equals to window because it follows the this from bar.让我们仔细看看如何使用箭头函数,以及在本课中如何使用箭头函数。
Let's take a closer look at how to use arrow functions and how this works with them in this lesson.注意:箭头函数不能通过使用bind()设置它们的自己this值。
Note: Arrow functions can never have their this value set using bind().利用新实现的publishedInYear,可以重写filter调用,从而消除箭头函数:.
With this new version of publishedInYear, we can rewrite our filter call,eliminating the arrow function:.在单行箭头函数(第5行)中,「return」关键字是隐式存在的,所以不需要显式写出来。
With single-line arrow functions(line 5), the'return' keyword is implicit so you don't need to type it out.即将到来的ECMAScript6增加了另外一种语法,称为箭头函数,允许我们编写更短的函数表达式。
Another syntax addition to the upcoming ECMAScript 6 is the arrow function expression to let us write a shorter function expression.有趣的是,使用箭头函数的部分应用比使用正常功能(至少在我们的微基准情况下)要快得多。
Using fat arrow functions is significantly faster than using normal functions(at least in our microbenchmark case).Yield关键字通常不能在箭头函数中使用(除非是嵌套在允许使用的函数内)。
In arrow functions, the keyword yield can't be used(except when normal functions are nested in it).
Please note that in for the arrow functions we need to use additional brackets.箭头函数的另一个优点就是如何管理“this”关键字。
The next benefit of arrow functions is how they manage the“this” keyword.所以在updateLanguage方法中使用箭头函数时我们不需要再担心this,意味着不必再使用.bind绑定它了。
So by using arrow functions in the updateLanguagemethod, we don't have to worry about this which means we don't have to call. bind anymore.我们不能完全跟上JavaScript的发展脚步,比如ES6--箭头函数、解构等等。
We didn't keep up with all the advancement in javascript e.g. ES6- arrow functions, deconstruction and so on.这就是为什么所有示例都是使用传统的function关键字编写的,而不是箭头函数。
This is why all of the examples are written using the traditional function keyword,instead arrow functions.Es6特性TypeScript包含了ECMAScript2015(es6,es7)的大部分特性,如类、接口、箭头函数等。
ES 6 Features: TypeScript includes most features of planned ECMAScript 2015(ES 6, 7) such as class,interface, Arrow functions etc.有两个因素会影响引入箭头函数:更简洁的函数和this。
Two factors influenced the introduction of arrow functions: shorter functions and non-binding of this.箭头函数中的super与stop()中的是相同的,所以它能按预期工作。
The super in the arrow function is the same as in stop(), so it works as intended.
At this point you maybe wondering what all the hype is about with arrow functions.这份规范花了7年才完成,但现在浏览器和其他运行运行环境终于开始支持箭头函数,let,const,代理和更多令人喜悦的特性。
The specification took seven years to complete,but browsers and runtimes are finally starting to support arrow functions, let, const, proxies and more delights.其中ES6版本(也称为ES2015)包括对前面版本的许多补充,例如:箭头函数、class、模板字面量、let和const语句。
The ES6 version(also known as ES2015)includes many additions to the previous versions such as: arrow functions, classes, template literals, let and const statements.
Arrow functions have no super.