-
-
Save botanicus/412946 to your computer and use it in GitHub Desktop.
JS: Object.prototype.extend()
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
var sys = require("sys"); | |
var extend = function extend (another) { | |
var prototype = this.__proto__; | |
this.__proto__ = another; | |
another.__proto__ = prototype; | |
return this; | |
}; | |
Object.defineProperty(Object.prototype, "extend", {value: extend }); | |
var Model = new Function(); | |
Model.prototype = { | |
saySomething: function() { | |
sys.puts("Yes, I am a model!"); | |
} | |
} | |
var NotModel = new Function(); | |
NotModel.prototype = { | |
saySomething: function() { | |
sys.puts("No, I am not a model!"); | |
} | |
} | |
var a = new Model(); | |
var b = new NotModel(); | |
sys.puts("Before extending:"); | |
sys.print("a: "); a.saySomething(); // Yes, I am a model! | |
sys.print("b: "); b.saySomething(); // No, I am not a model! | |
var SomeMixin = {}; | |
a.__proto__.extend(SomeMixin); | |
b.__proto__.extend(SomeMixin); | |
sys.puts("After extending:"); | |
sys.print("a: "); a.saySomething(); // No, I am not a model! | |
sys.print("b: "); b.saySomething(); // No, I am not a model! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment