Skip to content

Instantly share code, notes, and snippets.

@clehner
Created October 18, 2010 02:17
Show Gist options
  • Save clehner/631600 to your computer and use it in GitHub Desktop.
Save clehner/631600 to your computer and use it in GitHub Desktop.
Simple and Fast Javascript Inheritance. (No fancy stuff)
/*
Classy - Simple and Fast Javascript Inheritance.
(No fancy stuff)
Inspiration:
http://ejohn.org/blog/simple-javascript-inheritance/
http://valums.com/javascript-oop/
http://www.broofa.com/blog/2009/02/javascript-inheritance-performance/
Example:
Animal = Classy({
constructor: function () {
console.log("An animal is born.");
},
bite: function () {
alert("Bite!");
}
});
Cat = Classy(Animal, {
hiss: function () {
alert("Hisssss!");
},
biteAndHiss: function () {
Animal.prototype.bite.call(this, arguments);
this.hiss();
}
});
*/
function Classy(parent, properties) {
var newClass, prototype, prop;
// Dummy constructor. The reason this is used instead of the
// actual parent's constructor is that the parent's constructor
// should not be called when the child class is created.
// The parent argument is optional
if (!properties) {
prototype = parent;
parent = null;
}
else {
// Create the prototype, inheriting from parent.
function Tmp() {}
Tmp.prototype = parent.prototype;
prototype = new Tmp();
// Copy the properties into the new prototype.
for (prop in properties) {
prototype[prop] = properties[prop];
}
// Fix IE that doesn't iterate through non-enumerable properties
// when using for in loop.
/*@cc_on
var nonEnum = ["toString", "toLocaleString", "isPrototypeOf",
"propertyIsEnumerable", "hasOwnProperty", "valueOf", "constructor"];
while (prop = nonEnum.pop()) {
if (properties.hasOwnProperty(prop)) {
prototype[prop] = properties[prop];
}
}
@*/
}
// If the child has defined a constructor, use it.
if (prototype.hasOwnProperty("constructor")) {
newClass = prototype.constructor;
}
// Otherwise use the parent's constructor, or
// if there is no parent, use a dummy constructor
else {
if (parent) {
newClass = function () {
parent.apply(this, arguments);
};
}
else {
newClass = function () {};
}
prototype.constructor = newClass;
}
// Attach the prototype to the class.
newClass.prototype = prototype;
return newClass;
}
@clehner
Copy link
Author

clehner commented Oct 18, 2010

Compressed with closure compiler:

function Classy(d,e){var c,a,b;if(!e){a=d;d=null}else{function f(){}f.prototype=d.prototype;a=new f();for(b in e){a[b]=e[b]}}if(a.hasOwnProperty("constructor")){c=a.constructor}else{if(d){c=function(){d.apply(this,arguments)}}else{c=function(){}}a.constructor=c}c.prototype=a;/*@cc_on var g=["toString","toLocaleString","isPrototypeOf","propertyIsEnumerable","hasOwnProperty","valueOf"];while(b=g.pop()){if(e.hasOwnProperty(b)){a[b]=obj[b]}}@*/return c}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment