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