chuck.js/Factory.js
2012-07-19 15:38:17 +02:00

49 lines
No EOL
942 B
JavaScript

function Factory() {
this.notificationCenter = new NotificationCenter();
}
Factory.prototype.new = function () {
if (arguments.length < 1)
throw 'Too fiew arguments';
if (typeof arguments[0] != 'function')
throw arguments[0] + ' is not a function';
var module = arguments[0];
return Object.create(
module.prototype,
{
factory: {value: this},
notificationCenter: {value: this.notificationCenter}
}
);
}
function Player(name) {
this.name = name;
}
function NotificationCenter() {
this.foo = "a"
}
NotificationCenter.prototype.alert = function(a) {
console.log(a)
}
var factory = new Factory();
var player = factory.new(Player, "jeena");
console.log(player.notificationCenter);
/*
Lala:chuck.js jeena$ node Factory.js
Created NotificationCenter
Created Player
Player.notificationCenter [object Object]
Player.name jeena
*/