Skip to content
Open
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
.idea
62 changes: 62 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 10 additions & 9 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的指向是最靠近的父函数的作用域,即是say函数的调用对象
this.should.equal(obj);
done()
}, 0)
}
}
obj.say()
})
})

it('global', function () {
function test() {
// this 是什么?想想为什么?
this.should.equal(null)
// 全局调用
this.should.equal(global)
}
test()
})
Expand All @@ -25,8 +25,9 @@ describe('this', function () {
var obj = {
say: function () {
function _say() {
// this 是什么?想想为什么?
this.should.equal(null)
// 在定义函数的时候立即执行函数,虽然使用bind绑定obj对象,但是此时obj还未创建完毕,undefined
// bind在绑定的对象为undefined的时候,默认是绑定全局对象,此时执行的为node环境,所以为global
this.should.equal(global)
}
return _say.bind(obj)
}()
Expand All @@ -39,11 +40,11 @@ describe('this', function () {
obj.say = function () {
function _say() {
// this 是什么?想想为什么?
this.should.equal(null)
this.should.equal(obj)
}
return _say.bind(obj)
}()
obj.say()
})
})
})
})