I made a object to keep my functions became singleton, using that, i made sample methods to call and communicate each other.. but i don't get any appropriate results..
any one correct me, the singleton the way i defined here...
my sample codes:
var obj = window[obj] || {}; //singleton
obj.nameIt = function(name){
this.name = name;
this.getName = function(){
return this.name;
}
}
obj.sayIt = function(name){
this.name = name; var that = this;
this.sayHello = function(){
console.log("say" + this.name);
that.getName();//how to get result from nameIt?
}
}
var x = obj.nameIt("af");
console.log(x.getName());//says "undefined" - how to call it?
var y = obj.sayIt("xy");
console.log(y.sayHello());//says "undefined" - how to call it?
var x = obj.nameIt("af")here x is undefined because nameIt method returns nothing. See e.g: jsfiddle.net/PKhqF If you don't want to return something, then don't use variable, get directly obj property as:obj.namereturn this;to your methods. Besides, insayHelloyou won't get the name defined innameIt, because you have just overwritten it insayIt. You probably want anamevariable local tosayIt, see jsfiddle.net/szuwe/1