From 5e86d878493a2ed5af162dd9e0149e57b4ad3be1 Mon Sep 17 00:00:00 2001 From: pan463859 Date: Sat, 23 Mar 2019 18:53:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 3 +++ test/test.js | 19 ++++++++++--------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index dcdf033..9de598b 100644 --- a/package.json +++ b/package.json @@ -21,5 +21,8 @@ "homepage": "https://github.com/FE-star/exercise4#readme", "devDependencies": { "should": "^11.2.1" + }, + "dependencies": { + "mocha": "^6.0.2" } } diff --git a/test/test.js b/test/test.js index 63a72e4..e136fe2 100644 --- a/test/test.js +++ b/test/test.js @@ -3,19 +3,19 @@ describe('this', function () { var obj = { say: function () { setTimeout(() => { - // this 是什么?想想为什么? - this.should.equal(null) + // 箭头函数中的this指向定义时候的上下文对象 + this.should.equal(obj) done() }, 0) } } obj.say() - }) + }) it('global', function () { function test() { - // this 是什么?想想为什么? - this.should.equal(null) + // this的默认绑定,严格模式下为undefied,否则为全局对象 node中的全局对象为global + this.should.equal(global) } test() }) @@ -25,8 +25,9 @@ describe('this', function () { var obj = { say: function () { function _say() { - // this 是什么?想想为什么? - this.should.equal(null) + //call 和 bind 可以修改this指向 + //绑定时候的obj是undefiend,所以this指向了全局变量 + this.should.equal(global) } return _say.bind(obj) }() @@ -38,8 +39,8 @@ describe('this', function () { var obj = {} obj.say = function () { function _say() { - // this 是什么?想想为什么? - this.should.equal(null) + // this是obj,bind方法改变了当前的this指向 + this.should.equal(obj) } return _say.bind(obj) }()