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
24 changes: 13 additions & 11 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ describe('this', function () {
var obj = {
say: function () {
setTimeout(() => {
// this 是什么?想想为什么?
this.should.equal(null)
// this 是obj,obj.say()通过obj找到say(),所以是在obj环境执行,this 就是obj
this.should.equal(obj)
done()
}, 0)
}
}
obj.say()
})
})

it('global', function () {
function test() {
// this 是什么?想想为什么?
this.should.equal(null)
// this是global, 因为test()在全局环境执行
this.should.equal(global)
}
test()
})
Expand All @@ -25,10 +25,11 @@ describe('this', function () {
var obj = {
say: function () {
function _say() {
// this 是什么?想想为什么?
this.should.equal(null)
// this 是undefined
//根据函数优先原则:say和_say 函数声明都提升了,然后才提升obj。在say函数中调用obj时,obj还没有初始化
this.should.equal(undefined)
}
return _say.bind(obj)
return _say.bind(obj)//将_say的this绑定到obj
}()
}
obj.say()
Expand All @@ -38,10 +39,11 @@ describe('this', function () {
var obj = {}
obj.say = function () {
function _say() {
// this 是什么?想想为什么?
this.should.equal(null)
// this 是obj
//_say是函数声明被提升了,say函数表达式没有被提升。这时obj已经被创建并初始化了。将_say的this绑定到obj
this.should.equal(obj)
}
return _say.bind(obj)
return _say.bind(obj)//将_say的this绑定到obj
}()
obj.say()
})
Expand Down