Last active
February 1, 2024 15:59
-
-
Save CatTail/794af909953c29745793add4411ee0d5 to your computer and use it in GitHub Desktop.
Revisions
-
CatTail revised this gist
May 12, 2017 . 1 changed file with 2 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -24,9 +24,6 @@ function test (inherits) { assert.equal(fruit.constructor.prototype.constructor, Fruit) assert.equal(Object.getPrototypeOf(fruit), Fruit.prototype) // *prototype chain* assert.equal(Object.getPrototypeOf(apple), Apple.prototype) assert.equal(Object.getPrototypeOf(Object.getPrototypeOf(apple)), Fruit.prototype) @@ -41,6 +38,8 @@ function test (inherits) { Apple.prototype = {} assert.equal(apple.round, true) assert.equal(apple.sweet, true) // magic *__proto__* property assert.equal(Object.getPrototypeOf(fruit), fruit.__proto__) apple.__proto__ = {} assert.equal(apple.round, undefined) assert.equal(apple.sweet, undefined) -
CatTail revised this gist
May 11, 2017 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -30,6 +30,8 @@ function test (inherits) { // *prototype chain* assert.equal(Object.getPrototypeOf(apple), Apple.prototype) assert.equal(Object.getPrototypeOf(Object.getPrototypeOf(apple)), Fruit.prototype) assert.equal(Object.getPrototypeOf(Object.getPrototypeOf(Object.getPrototypeOf(apple))), Object.prototype) assert.equal(Object.getPrototypeOf(Object.getPrototypeOf(Object.getPrototypeOf(Object.getPrototypeOf(apple)))), null) // *property* assert.equal(fruit.round, false) -
CatTail revised this gist
May 11, 2017 . 1 changed file with 16 additions and 12 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -18,24 +18,24 @@ function test (inherits) { var fruit = new Fruit() var apple = new Apple() // *class*, *instance* and *prototype* assert.equal(fruit.constructor, Fruit) assert.equal(fruit.constructor.prototype, Fruit.prototype) assert.equal(fruit.constructor.prototype.constructor, Fruit) assert.equal(Object.getPrototypeOf(fruit), Fruit.prototype) // magic *__proto__* property assert.equal(Object.getPrototypeOf(fruit), fruit.__proto__) // *prototype chain* assert.equal(Object.getPrototypeOf(apple), Apple.prototype) assert.equal(Object.getPrototypeOf(Object.getPrototypeOf(apple)), Fruit.prototype) // *property* assert.equal(fruit.round, false) assert.equal(fruit.sweet, true) assert.equal(apple.round, true) assert.equal(apple.sweet, true) Apple.prototype = {} assert.equal(apple.round, true) assert.equal(apple.sweet, true) @@ -70,4 +70,8 @@ function inherits4 (constructor, superConstructor) { proto.constructor = constructor constructor.prototype = proto } test(inherits0) test(inherits1) test(inherits2) test(inherits3) test(inherits4) -
CatTail revised this gist
May 11, 2017 . 1 changed file with 7 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -18,13 +18,15 @@ function test (inherits) { var fruit = new Fruit() var apple = new Apple() // relationship between *class*, *instance* and *prototype* assert.equal(fruit.constructor, Fruit) assert.equal(fruit.constructor.prototype, Fruit.prototype) assert.equal(Object.getPrototypeOf(fruit), Fruit.prototype) assert.equal(fruit.__proto__, Fruit.prototype) assert.equal(fruit.round, false) assert.equal(fruit.sweet, true) // *inheritance* and *prototype chain* assert.equal(apple.constructor, Apple) assert.equal(apple.constructor.prototype, Apple.prototype) assert.equal(Object.getPrototypeOf(apple), Apple.prototype) @@ -33,20 +35,21 @@ function test (inherits) { assert.equal(apple.round, true) assert.equal(apple.sweet, true) // *prototype chain* and *property* Apple.prototype = {} assert.equal(apple.round, true) assert.equal(apple.sweet, true) apple.__proto__ = {} assert.equal(apple.round, undefined) assert.equal(apple.sweet, undefined) } var inherits0 = util.inherits function inherits1 (constructor, superConstructor) { // leverage *new* keyword to setup prototype var EmptyFunc = function () {} EmptyFunc.prototype = superConstructor.prototype var proto = new EmptyFunc() proto.constructor = constructor constructor.prototype = proto } -
CatTail revised this gist
May 11, 2017 . 1 changed file with 57 additions and 45 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,58 +1,70 @@ var assert = require('assert') var util = require('util') function test (inherits) { function Fruit () { } Fruit.prototype.round = false Fruit.prototype.sweet = true Fruit.prototype.eat = function () {} function Apple () { } inherits(Apple, Fruit) Apple.prototype.round = true var fruit = new Fruit() var apple = new Apple() assert.equal(fruit.constructor, Fruit) assert.equal(fruit.constructor.prototype, Fruit.prototype) assert.equal(Object.getPrototypeOf(fruit), Fruit.prototype) assert.equal(fruit.__proto__, Fruit.prototype) assert.equal(fruit.round, false) assert.equal(fruit.sweet, true) assert.equal(apple.constructor, Apple) assert.equal(apple.constructor.prototype, Apple.prototype) assert.equal(Object.getPrototypeOf(apple), Apple.prototype) assert.equal(Object.getPrototypeOf(apple.constructor.prototype), Fruit.prototype) assert.equal(apple.constructor.prototype.__proto__, Fruit.prototype) assert.equal(apple.round, true) assert.equal(apple.sweet, true) Apple.prototype = {} assert.equal(apple.round, true) assert.equal(apple.sweet, true) apple.__proto__ = {} assert.equal(apple.round, undefined) assert.equal(apple.sweet, undefined) } var inherits0 = util.inherits function inherits1 (constructor, superConstructor) { var Func = function () {} Func.prototype = superConstructor.prototype var proto = new Func() proto.constructor = constructor constructor.prototype = proto } function inherits2 (constructor, superConstructor) { var proto = Object.create(superConstructor.prototype) proto.constructor = constructor constructor.prototype = proto } function inherits3 (constructor, superConstructor) { var proto = {} Object.setPrototypeOf(proto, superConstructor.prototype) proto.constructor = constructor constructor.prototype = proto } function inherits4 (constructor, superConstructor) { var proto = {} proto.__proto__ = superConstructor.prototype proto.constructor = constructor constructor.prototype = proto } [inherits0, inherits1, inherits2, inherits3, inherits4].forEach(test) -
CatTail revised this gist
May 11, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -45,7 +45,7 @@ function inherits2 (constructor, superConstructor) { } function inherits3 (constructor, superConstructor) { var proto = {} Object.setPrototypeOf(proto, superConstructor.prototype) proto.constructor = constructor constructor.prototype = proto } -
CatTail created this gist
May 11, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,58 @@ var assert = require('assert') function test (inherits) { function Fruit () { } Fruit.prototype.sweet = true Fruit.prototype.eat = function () {} function Apple () { } inherits(Apple, Fruit) Apple.prototype.round = true var fruit = new Fruit() var apple = new Apple() assert.equal(fruit.constructor, Fruit) assert.equal(fruit.constructor.prototype, Fruit.prototype) assert.equal(Object.getPrototypeOf(fruit), Fruit.prototype) assert.equal(fruit.__proto__, Fruit.prototype) assert.equal(fruit.sweet, true) assert.equal(apple.constructor, Apple) assert.equal(apple.constructor.prototype, Apple.prototype) assert.equal(Object.getPrototypeOf(apple), Apple.prototype) assert.equal(Object.getPrototypeOf(apple.constructor.prototype), Fruit.prototype) assert.equal(apple.constructor.prototype.__proto__, Fruit.prototype) assert.equal(apple.round, true) assert.equal(apple.sweet, true) } function inherits1 (constructor, superConstructor) { var Func = function () {} Func.prototype = superConstructor.prototype var proto = new Func() proto.constructor = constructor constructor.prototype = proto } function inherits2 (constructor, superConstructor) { var proto = Object.create(superConstructor.prototype) proto.constructor = constructor constructor.prototype = proto } function inherits3 (constructor, superConstructor) { var proto = {} proto.__proto__ = superConstructor.prototype proto.constructor = constructor constructor.prototype = proto } function inherits4 (constructor, superConstructor) { var proto = {} proto.__proto__ = superConstructor.prototype proto.constructor = constructor constructor.prototype = proto } [inherits1, inherits2, inherits3, inherits4].forEach(test)