1
0
mirror of https://github.com/ruanyf/es6tutorial.git synced 2025-05-28 21:32:20 +00:00

edit function/function bind

This commit is contained in:
Ruan Yifeng 2015-06-10 11:58:31 +08:00
parent f3959a0e1f
commit a0e1c8d448

View File

@ -655,6 +655,26 @@ mult2(plus1(5))
```
## 函数绑定
箭头函数可以绑定this对象大大减少了显式绑定this对象的写法call、apply、bind。但是箭头函数并不适用于所有场合所以ES7提出了“函数绑定”function bind运算符用来取代call、apply、bind调用。虽然该语法还是ES7的一个提案但是Babel转码器已经支持。
函数绑定运算符是并排的两个双引号(::双引号左边是一个对象右边是一个函数。该运算符会自动将左边的对象作为上下文环境即this对象绑定到右边的函数上面。
```javascript
let log = ::console.log;
// 等同于
var log = console.log.bind(console);
foo::bar;
// 等同于
bar.call(foo);
foo::bar(...arguments);
// 等同于
bar.apply(foo, arguments);
```
## 尾调用优化
### 什么是尾调用?