From ad3ec82d5a6768622ab8b8aae13277c179726fcd Mon Sep 17 00:00:00 2001 From: herott <294963786@qq.com> Date: Sun, 4 Aug 2019 14:29:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E7=BB=83=E4=B9=A04?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/test.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) 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) }()