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
16 changes: 12 additions & 4 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ describe('this', function () {
say: function () {
setTimeout(() => {
// this 是什么?想想为什么?
this.should.equal(null)
// this 是 obj, 箭头函数中的this是在定义函数的时候绑定,而不是在执行函数时绑定
this.should.equal(obj)
done()
}, 0)
}
Expand All @@ -15,7 +16,8 @@ describe('this', function () {
it('global', function () {
function test() {
// this 是什么?想想为什么?
this.should.equal(null)
// this使用默认绑定(非严格模式),严格模式是undefined
this.should.equal(global)
}
test()
})
Expand All @@ -26,8 +28,13 @@ describe('this', function () {
say: function () {
function _say() {
// this 是什么?想想为什么?
this.should.equal(null)
// 《你不知道的js上》书中 96 页 2.4.1 被忽略的this,
// 如果你把null或者undefined作为this的绑定对象传如call,apply或者bind,
// 这些值在调用时会被忽略,实际使用的是默认绑定规则
// 显示绑定了obj,但是此时 obj 是 undefined, 所以 this 是 global(非严格模式),严格模式是undefined
this.should.equal(global)
}
console.log('obj:', obj)
return _say.bind(obj)
}()
}
Expand All @@ -39,7 +46,8 @@ describe('this', function () {
obj.say = function () {
function _say() {
// this 是什么?想想为什么?
this.should.equal(null)
// this 是 obj,同上一个,因为此时的obj已存在
this.should.equal(obj)
}
return _say.bind(obj)
}()
Expand Down