im learning more about javascript design patterns, so i been doing a example, in this case im creating a singleton, but im having some problems in one function called "change", is giving me a error saying the change is not a function, i dont understand why since is nothing difference from the others functions.
var singleton = (function(){
var _counter;
return{
create: function (tagName, id){
var el = document.createElement(tagName);
el.id = id || "Custom ID-"+this.generateID();
return el;
},
generateID : function(){
if(!_counter){
_counter = 1;
return _counter;
}else{
return _counter;
}
},
change : function(id){
_counter = id;
}
}
}());
Creating objects:
var test1 = singleton.create("div"); (output: <div id="Custom ID-1"></div>)
var test2 = singleton.create("span");(<span id="Custom ID-1"></span>
test1.change("2");
Aucun commentaire:
Enregistrer un commentaire