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.

94 changes: 51 additions & 43 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,57 @@
// 测试套件
describe('this', function () {
it('setTimeout', function (done) {
var obj = {
say: function () {
setTimeout(() => {
// this 是什么?想想为什么?
this.should.equal(null)
done()
}, 0)
}
}
obj.say()
})

it('global', function () {
function test() {
// this 是什么?想想为什么?
this.should.equal(null)
}
test()
})
it('setTimeout', function (done) {
var obj = {
say: function () {
setTimeout(() => {
// this 是什么?想想为什么?
// this.should.equal(null)
this.should.equal(obj)
done()
}, 0)
}
}
obj.say()
})

describe('bind', function () {
it('bind undefined', function () {
var obj = {
say: function () {
function _say() {
it('global', function () {
function test() {
// this 是什么?想想为什么?
this.should.equal(null)
}
return _say.bind(obj)
}()
}
obj.say()
// this.should.equal(null)
// 非严格模式下, 在node 中 this指向 global 全局对象
// console.log(global);
this.should.equal(global)
}

test()
})

it('bind normal', function () {
var obj = {}
obj.say = function () {
function _say() {
// this 是什么?想想为什么?
this.should.equal(null)
}
return _say.bind(obj)
}()
obj.say()
describe('bind', function () {
it('bind undefined', function () {
var obj = {
say: function () {
function _say() {
// this 是什么?想想为什么?
this.should.equal(global)
}
// 函数立即执行
return _say.bind(obj)
}()
}
obj.say()
})

it('bind normal', function () {
var obj = {}
obj.say = function () {
function _say() {
// this 是什么?想想为什么?
this.should.equal(obj)
}

return _say.bind(obj)
}()
obj.say()
})
})
})
})
})