From df6c8bf0e2f4be53f689c9ce89c973062b7dd762 Mon Sep 17 00:00:00 2001 From: Jeena Paradies Date: Thu, 19 Jul 2012 15:00:36 +0200 Subject: [PATCH] changed to extnsible object --- Factory.js | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/Factory.js b/Factory.js index 3b9e1d9..08acac4 100644 --- a/Factory.js +++ b/Factory.js @@ -9,19 +9,30 @@ Factory.prototype.new = function () { if (typeof arguments[0] != 'function') throw arguments[0] + ' is not a function'; - var klass = arguments[0]; - klass.prototype = new ChuckObject(this.notificationCenter, this); - return new (klass.bind.apply(klass,arguments))(); + var module = arguments[0]; + module.prototype = new ExtensibleObject({ + notificationCenter: this.notificationCenter, + factory: this + }); + + return new (module.bind.apply(module, arguments))(); } -function ChuckObject(notificationCenter, factory) { - this.notificationCenter = notificationCenter; - this.factory = factory; +function ExtensibleObject(properties) { + for(var propertyName in properties) { + this.__defineGetter__(propertyName, function() { + return properties[propertyName] + }); + + this.__defineSetter__(propertyName, function(val) { + properties[propertyName] = val; + }); + } } function Player(name) { this.name = name; - console.log("Created Player"); + console.log("Created Player: " + name); console.log("Player.notificationCenter " + this.notificationCenter); } @@ -29,10 +40,15 @@ function NotificationCenter() { console.log("Created NotificationCenter"); } +NotificationCenter.prototype.alert = function(a) { + console.log(a) +} + var factory = new Factory(); var player = factory.new(Player, "jeena"); console.log("Player.name " + player.name); +console.log("New player name: " + player.factory.new(Player, "logsol").notificationCenter.alert); /*