Created
June 2, 2015 13:12
-
-
Save droxer/9f9a259fcc3c0995f8c8 to your computer and use it in GitHub Desktop.
javascript inherit.js
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 characters
| #!/usr/local/bin/node | |
| var assert = require('assert') | |
| function Animal (name) { | |
| this.name = name; | |
| } | |
| assert.equal(new Animal("animal").constructor, Animal) | |
| assert.equal(new Animal("animal") instanceof Animal, true) | |
| Animal.prototype = { | |
| getName: function () { | |
| return this.name | |
| } | |
| } | |
| assert.equal(new Animal("animal").constructor, Object) | |
| assert.equal(new Animal("animal") instanceof Animal, true) | |
| Animal.prototype.constructor = Animal | |
| assert.equal(new Animal("animal").constructor, Animal) | |
| function Cat(name) { | |
| Animal.call(this, name); | |
| // Animal.apply(this, name); | |
| } | |
| Cat.prototype = new Animal() | |
| Cat.prototype.getName = function () { | |
| return this.name | |
| } | |
| assert.equal(new Cat("animal").constructor, Animal) | |
| assert.equal(new Cat("animal") instanceof Cat, true) | |
| Cat.prototype.constructor = Cat | |
| assert.equal(new Cat("animal").constructor, Cat) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment