protoypal-inheritance
var charlie = new Actor();
function Actor(name) {
this.name = name;
}
Actor.prototype.act = function(line) {
alert(this.name + ': ' + line);
};
function Actor(name) {
this.name = name;
}
Actor.prototype.canSpeak = true;
function SilentActor() {
Actor.apply(this, arguments);
}
SilentActor.prototype = new Actor();
SilentActor.prototype.canSpeak = false;
///Our 'actor' object has some properties...
var actor = {
canAct: true,
canSpeak: true
};
var silentActor = Object.create(actor); ///'silentActor' inherits from 'actor'
silentActor.canSpeak = false;
///'busterKeaton' inherits from 'silentActor'
var busterKeaton = Object.create(silentActor);
Object.getPrototypeOf(busterKeaton); // silentActor
Object.getPrototypeOf(silentActor); // actor
Object.getPrototypeOf(actor); // Object
This Gist was automatically created by Carbide, a free online programming environment.