Skip to content

Instantly share code, notes, and snippets.

@droxer
Created June 2, 2015 13:12
Show Gist options
  • Save droxer/9f9a259fcc3c0995f8c8 to your computer and use it in GitHub Desktop.
Save droxer/9f9a259fcc3c0995f8c8 to your computer and use it in GitHub Desktop.
javascript inherit.js
#!/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