Skip to content

Commit 200b44e

Browse files
committed
docs(oop/this): fixed #279
1 parent 5aa689b commit 200b44e

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

Diff for: docs/oop/this.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -679,19 +679,19 @@ Array.prototype.slice.apply({length: 1}) // [undefined]
679679
前面的按钮点击事件的例子,可以改写如下。
680680

681681
```javascript
682-
var o = new Object();
682+
var obj = new Object();
683683

684-
o.f = function () {
685-
console.log(this === o);
684+
var func = function () {
685+
console.log(this === obj);
686686
}
687687

688-
var f = function (){
689-
o.f.apply(o);
690-
// 或者 o.f.call(o);
688+
var handler = function (){
689+
func.apply(obj);
690+
// 或者 f.call(obj);
691691
};
692692

693693
// jQuery 的写法
694-
$('#button').on('click', f);
694+
$('#button').on('click', handler);
695695
```
696696

697697
上面代码中,点击按钮以后,控制台将会显示`true`。由于`apply()`方法(或者`call()`方法)不仅绑定函数执行时所在的对象,还会立即执行函数,因此不得不把绑定语句写在一个函数体内。更简洁的写法是采用下面介绍的`bind()`方法。

0 commit comments

Comments
 (0)