diff --git a/Factory.js b/Factory.js index f011a8b..3cebe6a 100644 --- a/Factory.js +++ b/Factory.js @@ -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"); /*