Subclasses in Javascript
June 10, 2014
If you aren’t spoiled by Coffeescript or a framework like Backbone, subclassing in Javascript isn’t straight forward. Check this out:
// Base Beatle class
function Beatle(name) {
this.name = name;
}
Beatle.prototype.sayHello = function () {
alert(this.name + ' wore Beatle boots.');
}
// Ringo, a Beatle subclass and drummer
function Ringo(){
Beatle.call(this, 'Ringo');
}
// Use Object.create to break reference to parent's prototype.
Ringo.prototype = Object.create(Beatle.prototype);
Ringo.prototype.constructor = Ringo;
Ringo.prototype.sayHello = function() {
alert('Peace and love!');
}
Hey, I'm Ian. I build websites and write about what I learn as I go. Follow me on Twitter.