diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..508cf54 --- /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": "https://registry.npmjs.org/should/-/should-11.2.1.tgz", + "integrity": "sha1-kPVRRVUtAc/CAGZuToGKHJZw7aI=", + "dev": true, + "requires": { + "should-equal": "1.0.1", + "should-format": "3.0.3", + "should-type": "1.4.0", + "should-type-adaptors": "1.1.0", + "should-util": "1.0.0" + } + }, + "should-equal": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/should-equal/-/should-equal-1.0.1.tgz", + "integrity": "sha1-C26VFvJgGp+wuy3MNpr6HH4gCvc=", + "dev": true, + "requires": { + "should-type": "1.4.0" + } + }, + "should-format": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/should-format/-/should-format-3.0.3.tgz", + "integrity": "sha1-m/yPdPo5IFxT04w01xcwPidxJPE=", + "dev": true, + "requires": { + "should-type": "1.4.0", + "should-type-adaptors": "1.1.0" + } + }, + "should-type": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/should-type/-/should-type-1.4.0.tgz", + "integrity": "sha1-B1bYzoRt/QmEOmlHcZ36DUz/XPM=", + "dev": true + }, + "should-type-adaptors": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/should-type-adaptors/-/should-type-adaptors-1.1.0.tgz", + "integrity": "sha512-JA4hdoLnN+kebEp2Vs8eBe9g7uy0zbRo+RMcU0EsNy+R+k049Ki+N5tT5Jagst2g7EAja+euFuoXFCa8vIklfA==", + "dev": true, + "requires": { + "should-type": "1.4.0", + "should-util": "1.0.0" + } + }, + "should-util": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/should-util/-/should-util-1.0.0.tgz", + "integrity": "sha1-yYzaN0qmsZDfi6h8mInCtNtiAGM=", + "dev": true + } + } +} diff --git a/test/test.js b/test/test.js index 63a72e4..bf5beb3 100644 --- a/test/test.js +++ b/test/test.js @@ -1,49 +1,57 @@ +// 测试套件 describe('this', function () { - it('setTimeout', function (done) { - var obj = { - say: function () { - setTimeout(() => { - // this 是什么?想想为什么? - this.should.equal(null) - done() - }, 0) - } - } - obj.say() - }) - - it('global', function () { - function test() { - // this 是什么?想想为什么? - this.should.equal(null) - } - test() - }) + it('setTimeout', function (done) { + var obj = { + say: function () { + setTimeout(() => { + // this 是什么?想想为什么? + // this.should.equal(null) + this.should.equal(obj) + done() + }, 0) + } + } + obj.say() + }) - describe('bind', function () { - it('bind undefined', function () { - var obj = { - say: function () { - function _say() { + it('global', function () { + function test() { // this 是什么?想想为什么? - this.should.equal(null) - } - return _say.bind(obj) - }() - } - obj.say() + // this.should.equal(null) + // 非严格模式下, 在node 中 this指向 global 全局对象 + // console.log(global); + this.should.equal(global) + } + + test() }) - it('bind normal', function () { - var obj = {} - obj.say = function () { - function _say() { - // this 是什么?想想为什么? - this.should.equal(null) - } - return _say.bind(obj) - }() - obj.say() + describe('bind', function () { + it('bind undefined', function () { + var obj = { + say: function () { + function _say() { + // this 是什么?想想为什么? + this.should.equal(global) + } + // 函数立即执行 + return _say.bind(obj) + }() + } + obj.say() + }) + + it('bind normal', function () { + var obj = {} + obj.say = function () { + function _say() { + // this 是什么?想想为什么? + this.should.equal(obj) + } + + return _say.bind(obj) + }() + obj.say() + }) }) - }) -}) \ No newline at end of file +})