Skip to content

Instantly share code, notes, and snippets.

@christopherscott
Created February 11, 2013 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christopherscott/4758081 to your computer and use it in GitHub Desktop.
Save christopherscott/4758081 to your computer and use it in GitHub Desktop.
Prototypical inheritance in JavaScript with 'classes' and 'instances'
var Lib = {
extend: function (props) {
var sub = mixin(Object.create(this), props);
sub.super = this;
return sub;
},
create: function () {
var instance = Object.create(this);
if (instance.init) { instance.init.apply(instance, arguments); }
return instance;
}
};
var Person = Lib.extend({
init: function (name) {
this.name = name;
},
speak: function () {
console.log(this.name);
}
});
var Employee = Person.extend({
work: function () {
console.log(this.name + ' is working');
}
});
var c = Employee.create('chris');
c.speak();
c.work();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment