Created
December 12, 2012 02:05
-
-
Save xphere/4264246 to your computer and use it in GitHub Desktop.
Extend JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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