diff --git a/test/test.js b/test/test.js index 63a72e4..26a3231 100644 --- a/test/test.js +++ b/test/test.js @@ -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) } @@ -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() }) @@ -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) }() @@ -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) }()