diff --git a/.gitignore b/.gitignore index b512c09..eb79dd5 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -node_modules \ No newline at end of file +node_modules +.idea diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..4742ba2 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,62 @@ +{ + "name": "exercise4", + "version": "0.0.1", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "should": { + "version": "11.2.1", + "resolved": "http://registry.npm.taobao.org/should/download/should-11.2.1.tgz", + "integrity": "sha1-kPVRRVUtAc/CAGZuToGKHJZw7aI=", + "dev": true, + "requires": { + "should-equal": "^1.0.0", + "should-format": "^3.0.2", + "should-type": "^1.4.0", + "should-type-adaptors": "^1.0.1", + "should-util": "^1.0.0" + } + }, + "should-equal": { + "version": "1.0.1", + "resolved": "http://registry.npm.taobao.org/should-equal/download/should-equal-1.0.1.tgz", + "integrity": "sha1-C26VFvJgGp+wuy3MNpr6HH4gCvc=", + "dev": true, + "requires": { + "should-type": "^1.0.0" + } + }, + "should-format": { + "version": "3.0.3", + "resolved": "http://registry.npm.taobao.org/should-format/download/should-format-3.0.3.tgz", + "integrity": "sha1-m/yPdPo5IFxT04w01xcwPidxJPE=", + "dev": true, + "requires": { + "should-type": "^1.3.0", + "should-type-adaptors": "^1.0.1" + } + }, + "should-type": { + "version": "1.4.0", + "resolved": "http://registry.npm.taobao.org/should-type/download/should-type-1.4.0.tgz", + "integrity": "sha1-B1bYzoRt/QmEOmlHcZ36DUz/XPM=", + "dev": true + }, + "should-type-adaptors": { + "version": "1.1.0", + "resolved": "http://registry.npm.taobao.org/should-type-adaptors/download/should-type-adaptors-1.1.0.tgz", + "integrity": "sha1-QB5/M7VTMDOUTVzYvytlAneS4no=", + "dev": true, + "requires": { + "should-type": "^1.3.0", + "should-util": "^1.0.0" + } + }, + "should-util": { + "version": "1.0.0", + "resolved": "http://registry.npm.taobao.org/should-util/download/should-util-1.0.0.tgz", + "integrity": "sha1-yYzaN0qmsZDfi6h8mInCtNtiAGM=", + "dev": true + } + } +} diff --git a/test/test.js b/test/test.js index 63a72e4..41d4dd8 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的指向是最靠近的父函数的作用域,即是say函数的调用对象 + this.should.equal(obj); done() }, 0) } } obj.say() - }) + }) it('global', function () { function test() { - // this 是什么?想想为什么? - this.should.equal(null) + // 全局调用 + this.should.equal(global) } test() }) @@ -25,8 +25,9 @@ describe('this', function () { var obj = { say: function () { function _say() { - // this 是什么?想想为什么? - this.should.equal(null) + // 在定义函数的时候立即执行函数,虽然使用bind绑定obj对象,但是此时obj还未创建完毕,undefined + // bind在绑定的对象为undefined的时候,默认是绑定全局对象,此时执行的为node环境,所以为global + this.should.equal(global) } return _say.bind(obj) }() @@ -39,11 +40,11 @@ describe('this', function () { obj.say = function () { function _say() { // this 是什么?想想为什么? - this.should.equal(null) + this.should.equal(obj) } return _say.bind(obj) }() obj.say() }) }) -}) \ No newline at end of file +})