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
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,所以内部的this就是外层代码块的this,因此这块的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指向为调用它的对象,test()为全局调用函数,如果是nodejs那么全局是global,浏览器在严格模式下为undefined
this.should.equal(undefined)
}
test()
})
Expand All @@ -26,7 +28,8 @@ describe('this', function () {
say: function () {
function _say() {
// this 是什么?想想为什么?
this.should.equal(null)
// bind改变this的指向为obj但是obj声明了没有赋值,所以this的指向为undefined
this.should.equal(undefined)
}
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