Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
/* eslint-env mocha */
describe('this', function () {
it('setTimeout', function (done) {
var obj = {
say: function () {
setTimeout(() => {
// this 是什么?想想为什么?
this.should.equal(null)
this.should.equal(obj)
// this 是一个完全根据调用点(函数是如何被调用的)而为每次函数调用建立的绑定
done()
}, 0)
}
}
obj.say()
// obj.say.call(obj)
})

it('global', function () {
function test() {
// this 是什么?想想为什么?
this.should.equal(null)
this.should.equal(undefined)
// this 是一个完全根据调用点(函数是如何被调用的)而为每次函数调用建立的绑定
// 在全局中调用,全局对象node中是global,在浏览器中是Window
}
test()
//test.call(undefined)
})

describe('bind', function () {
Expand All @@ -26,7 +32,9 @@ describe('this', function () {
say: function () {
function _say() {
// this 是什么?想想为什么?
this.should.equal(null)
this.should.equal(undefined)
// 全局对象node中是global,在浏览器中是Window
// 用bind方法来设置函数的this值,但是say是一个obj内部的立即执行函数,绑定时obj还是undefined
}
return _say.bind(obj)
}()
Expand All @@ -39,7 +47,9 @@ describe('this', function () {
obj.say = function () {
function _say() {
// this 是什么?想想为什么?
this.should.equal(null)
this.should.equal(obj)
// 用bind方法来设置函数的this值,而不用考虑函数如何被调用的
// 和上一个例子一样也是一个立即执行函数,但是obj一开始被创建出来了,所以绑定到了obj
}
return _say.bind(obj)
}()
Expand Down