Skip to content

Instantly share code, notes, and snippets.

@phaistonian
Created May 7, 2013 15:56
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 phaistonian/5533734 to your computer and use it in GitHub Desktop.
Save phaistonian/5533734 to your computer and use it in GitHub Desktop.
SubClass.js
subClass = function (source, prototype) {
var dummy = new source.constructor;
for (var key in prototype) {
dummy[key] = prototype[key];
}
source.__proto__ = dummy;
}
// Array
source = [1,2,3];
prototype = {
alert : function () {
alert(this);
}
}
subClass(source, prototype);
source.alert(); // 1, 2, 3
[1, 2, 3].alert(); // undefined
// Date
source = new Date();
prototype = {
alert : function () {
alert(this);
}
}
subClass(source, prototype);
source.alert(); // Date
new Date().alert(); // undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment