now with Object.create() again but working

This commit is contained in:
Jeena Paradies 2012-07-19 18:02:57 +02:00
parent 976bf12cbe
commit 2ab87cb063

View file

@ -2,38 +2,36 @@ function Factory() {
this.notificationCenter = new NotificationCenter(); this.notificationCenter = new NotificationCenter();
} }
Factory.prototype.new = function () { Factory.prototype.new = function (constructor /*, arg1, arg2, ... */) {
if (arguments.length < 1) if (arguments.length < 1)
throw 'Too fiew arguments'; throw 'Too fiew arguments';
if (typeof arguments[0] != 'function') if (typeof arguments[0] != 'function')
throw arguments[0] + ' is not a function'; throw arguments[0] + ' is not a function';
var module = arguments[0]; var instance = Object.create(constructor.prototype, {
notificationCenter: {
module.prototype = new ExtensibleObject({ value: this.notificationCenter
factory: this, },
notificationCenter: this.notificationCenter factory: {
value: this
}
}); });
return new (module.bind.apply(module, arguments))(); constructor.apply(instance, Array.prototype.slice.call(arguments, [1]));
} return instance;
function ExtensibleObject(properties) {
for(var propertyName in properties) {
this[propertyName] = properties[propertyName];
}
} }
function Player(name) { function Player(name) {
this.name = name; this.name = name;
this.notificationCenter.log("Player " + name + " created")
} }
function NotificationCenter() { function NotificationCenter() {
this.foo = "a" this.foo = "a"
} }
NotificationCenter.prototype.alert = function(a) { NotificationCenter.prototype.log = function(a) {
console.log(a) console.log(a)
} }
@ -41,7 +39,7 @@ var factory = new Factory();
var player = factory.new(Player, "jeena"); var player = factory.new(Player, "jeena");
player.factory.new(Player, "logsol").notificationCenter.alert("foo"); player.factory.new(Player, "logsol").notificationCenter.log("test");
/* /*