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.

14 changes: 10 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)
// answer: 隐含绑定(Implicit Binding)
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)
// answer: 默认绑定(Default Binding)
this.should.equal(global)
}
test()
})
Expand All @@ -26,7 +28,9 @@ describe('this', function () {
say: function () {
function _say() {
// this 是什么?想想为什么?
this.should.equal(null)
// answer: 1. 立即执行函数 -> 明确绑定了obj, 此时obj已经被声明提升,但还未赋值,是undefined
// 2. 而bind undefined,会把this绑定global
this.should.equal(global)
}
return _say.bind(obj)
}()
Expand All @@ -39,7 +43,9 @@ describe('this', function () {
obj.say = function () {
function _say() {
// this 是什么?想想为什么?
this.should.equal(null)
// answer: 1. 立即执行函数 -> 明确绑定了obj(this指向obj的地址,此时obj为{}),
// 2. obj.say()执行 -> 此时obj的值被改变(值为{ say: [Function: bound _say] })
this.should.equal(obj)
}
return _say.bind(obj)
}()
Expand Down