Skip to content

Commit a0e1c8d

Browse files
committed
edit function/function bind
1 parent f3959a0 commit a0e1c8d

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

docs/function.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,26 @@ mult2(plus1(5))
655655

656656
```
657657

658+
## 函数绑定
659+
660+
箭头函数可以绑定this对象,大大减少了显式绑定this对象的写法(call、apply、bind)。但是,箭头函数并不适用于所有场合,所以ES7提出了“函数绑定”(function bind)运算符,用来取代call、apply、bind调用。虽然该语法还是ES7的一个提案,但是Babel转码器已经支持。
661+
662+
函数绑定运算符是并排的两个双引号(::),双引号左边是一个对象,右边是一个函数。该运算符会自动将左边的对象,作为上下文环境(即this对象),绑定到右边的函数上面。
663+
664+
```javascript
665+
let log = ::console.log;
666+
// 等同于
667+
var log = console.log.bind(console);
668+
669+
foo::bar;
670+
// 等同于
671+
bar.call(foo);
672+
673+
foo::bar(...arguments);
674+
// 等同于
675+
bar.apply(foo, arguments);
676+
```
677+
658678
## 尾调用优化
659679

660680
### 什么是尾调用?

0 commit comments

Comments
 (0)