martedì, gennaio 22, 2008

[jsn]A simple JS IoC example code

Inspired by Ninject documentation I decided to port the c# example code to my JS IoC.
Here are the code:

var
shuriken = {
hit:function(who){alert("pierced the "+who+" armor");}
},

sword = {
hit:function(who){alert("Chopped the "+who+" in half");}
};

function Samurai(weapon){
return {
Attack:function(who){
weapon.hit(who);
}
}
}
container.register("samurai",Samurai);
var warrior1 = container.byConstructor("samurai",shuriken),
warrior2 = container.byConstructor("samurai",sword);

warrior1.Attack("The evildoers");
warrior2.Attack("The evildoers");


So simple, So clear!
Posta un commento