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
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.

12 changes: 8 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指向say,箭头函数是根据当前的词法作用域来决定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指向window,这个是使用了默认绑定,绑定到了全局对象。node环境中为global
this.should.equal(global)
}
test()
})
Expand All @@ -26,7 +28,8 @@ describe('this', function () {
say: function () {
function _say() {
// this 是什么?想想为什么?
this.should.equal(null)
// bind在绑定的对象为undefined的时候,默认是绑定全局对象
this.should.equal(global)
}
return _say.bind(obj)
}()
Expand All @@ -39,7 +42,8 @@ describe('this', function () {
obj.say = function () {
function _say() {
// this 是什么?想想为什么?
this.should.equal(null)
// bind 把this指向了obj
this.should.equal(obj)
}
return _say.bind(obj)
}()
Expand Down