Skip to content

Instantly share code, notes, and snippets.

@CatTail
Last active February 1, 2024 15:59
Show Gist options
  • Save CatTail/794af909953c29745793add4411ee0d5 to your computer and use it in GitHub Desktop.
Save CatTail/794af909953c29745793add4411ee0d5 to your computer and use it in GitHub Desktop.

Revisions

  1. CatTail revised this gist May 12, 2017. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions proto.js
    Original 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)

    // 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)
    @@ -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)
  2. CatTail revised this gist May 11, 2017. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions proto.js
    Original 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)
  3. CatTail revised this gist May 11, 2017. 1 changed file with 16 additions and 12 deletions.
    28 changes: 16 additions & 12 deletions proto.js
    Original file line number Diff line number Diff line change
    @@ -18,24 +18,24 @@ function test (inherits) {
    var fruit = new Fruit()
    var apple = new Apple()

    // relationship between *class*, *instance* and *prototype*
    // *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)
    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)
    // magic *__proto__* property
    assert.equal(Object.getPrototypeOf(fruit), fruit.__proto__)

    // *prototype chain*
    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(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)

    // *prototype chain* and *property*
    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
    }
    [inherits0, inherits1, inherits2, inherits3, inherits4].forEach(test)
    test(inherits0)
    test(inherits1)
    test(inherits2)
    test(inherits3)
    test(inherits4)
  4. CatTail revised this gist May 11, 2017. 1 changed file with 7 additions and 4 deletions.
    11 changes: 7 additions & 4 deletions proto.js
    Original 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) {
    var Func = function () {}
    Func.prototype = superConstructor.prototype
    var proto = new Func()
    // leverage *new* keyword to setup prototype
    var EmptyFunc = function () {}
    EmptyFunc.prototype = superConstructor.prototype
    var proto = new EmptyFunc()
    proto.constructor = constructor
    constructor.prototype = proto
    }
  5. CatTail revised this gist May 11, 2017. 1 changed file with 57 additions and 45 deletions.
    102 changes: 57 additions & 45 deletions proto.js
    Original 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.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 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
    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
    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
    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
    var proto = {}
    proto.__proto__ = superConstructor.prototype
    proto.constructor = constructor
    constructor.prototype = proto
    }
    [inherits1, inherits2, inherits3, inherits4].forEach(test)
    [inherits0, inherits1, inherits2, inherits3, inherits4].forEach(test)
  6. CatTail revised this gist May 11, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion proto.js
    Original file line number Diff line number Diff line change
    @@ -45,7 +45,7 @@ function inherits2 (constructor, superConstructor) {
    }
    function inherits3 (constructor, superConstructor) {
    var proto = {}
    proto.__proto__ = superConstructor.prototype
    Object.setPrototypeOf(proto, superConstructor.prototype)
    proto.constructor = constructor
    constructor.prototype = proto
    }
  7. CatTail created this gist May 11, 2017.
    58 changes: 58 additions & 0 deletions proto.js
    Original 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)