Skip to content

Instantly share code, notes, and snippets.

@pdtaylor

pdtaylor/example2.js

Last active Aug 29, 2015
Embed
What would you like to do?
Example 2
(function(){ //opens the closure
//utility function
function extractCode(obj){
return obj.code;
};
//definition of private object Animal
var Animal= function(){
this.name="";
};
Animal.prototype={
getAnimal : function(code){
if(code=="c"){
this.name="Cat";
}
else if(code=="d"){
this.name="Dog";
}
else{
this.name="unknown";
}
}
};
//myLib public interface
window.myLib = {
selectAnimal : function(params){
var selector=new Animal();
var code = extractCode(params);
selector.getAnimal(code);
return selector.name;
}
};
})(); //closes the closure
//Call selectAnimal publicly with JSON data
window.myLib.selectAnimal({"code":"d"});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.