Skip to content

Instantly share code, notes, and snippets.

@xphere
Created December 12, 2012 02:05
Show Gist options
  • Save xphere/4264246 to your computer and use it in GitHub Desktop.
Save xphere/4264246 to your computer and use it in GitHub Desktop.
Extend JS
function extend(object) {
if (arguments.length > 1) {
for (var i = 1; i < arguments.length; ++i) {
for (j in arguments[i]) {
object[j] = arguments[i][j];
}
}
}
return object;
}
function MyClass() {
}
extend(MyClass.prototype, {
method: function() { return 10 }
});
o = new MyClass();
console.log(o.method()); // Outputs 10
extend(o, {
other: function() { return 20 },
method: function() { return 30 }
});
console.log(o.method()); // Outputs 30
console.log(o.other()); // Outputs 20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment