mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 10:37:34 +00:00
now with Object.create() again but working
This commit is contained in:
parent
976bf12cbe
commit
2ab87cb063
1 changed files with 14 additions and 16 deletions
30
Factory.js
30
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");
|
||||
|
||||
|
||||
/*
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue