Created
October 10, 2012 03:08
-
-
Save milushov/3862938 to your computer and use it in GitHub Desktop.
js inheritance
This file contains 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
var extend = function(child, parent) { | |
for (var key in parent) { | |
if(parent.hasOwnProperty(key)) child[key] = parent[key]; | |
} | |
function ctor() { | |
this.constructor = child; | |
} | |
ctor.prototype = parent.prototype; | |
child.prototype = new ctor(); | |
child.__super__ = parent.prototype; | |
return child; | |
}; | |
function Animal(name) { | |
this.name = name; | |
} | |
Animal.prototype.move = function(meters) { | |
return console.log(this.name + (" moved " + meters + "m.")); | |
}; | |
extend(Snake, Animal); | |
function Snake() { | |
return Snake.__super__.constructor.apply(this, arguments); | |
} | |
Snake.prototype.move = function() { | |
console.log('Slithering...'); | |
return Snake.__super__.move.call(this, 23); | |
}; | |
var sam = new Snake('Sam'); | |
sam.move(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
вот оно какое, наследование в js..