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:
parent
f3959a0e1f
commit
a0e1c8d448
@ -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);
|
||||
```
|
||||
|
||||
## 尾调用优化
|
||||
|
||||
### 什么是尾调用?
|
||||
|
Loading…
x
Reference in New Issue
Block a user