diff --git a/test/test.js b/test/test.js index 63a72e4..cc559cb 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是函数调用时所处的上下文,又因为say是作为obj的属性调用的,故为obj + this.should.equal(obj) done() }, 0) } @@ -15,7 +16,8 @@ describe('this', function () { it('global', function () { function test() { // this 是什么?想想为什么? - this.should.equal(null) + // 默认绑定在window上,node环境下为global + this.should.equal(global) } test() }) @@ -26,7 +28,8 @@ describe('this', function () { say: function () { function _say() { // this 是什么?想想为什么? - this.should.equal(null) + // 显示绑定在obj上,obj为undefined,则绑定在global下 + this.should.equal(global) } return _say.bind(obj) }() @@ -39,7 +42,8 @@ describe('this', function () { obj.say = function () { function _say() { // this 是什么?想想为什么? - this.should.equal(null) + // 显示绑定在obj上 + this.should.equal(obj) } return _say.bind(obj) }()