Skip to content

Instantly share code, notes, and snippets.

@botanicus
Forked from rkh/mixin.js
Created May 25, 2010 09:13
Show Gist options
  • Save botanicus/412946 to your computer and use it in GitHub Desktop.
Save botanicus/412946 to your computer and use it in GitHub Desktop.
JS: Object.prototype.extend()
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