Skip to content

Instantly share code, notes, and snippets.

@pdtaylor
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pdtaylor/77b2634498a0a6c1b2c1 to your computer and use it in GitHub Desktop.
Save pdtaylor/77b2634498a0a6c1b2c1 to your computer and use it in GitHub Desktop.
Example 4
(function(){ //opens the closure
//definition of private object Animal
var Animal= function(){
this.name="";
};
Animal.prototype={
getAnimal : function(code){
//AJAX call to get the description
$.ajax({
url : "getDescription.json",
dataType : "json",
data : JSON.stringify({"code" : code}),
success : function(response){
this.name=response;
}
});
}
};
//myLib public interface
window.myLib = {
selector : null,
initialize : function(){
//public property instantiates an instance of Animal
this.selector= new Animal();
},
selectAnimal : function(code){
this.selector.getAnimal(code);
return this.selector.name;
}
};
})(); //closes the closure
//initializes selector publicly
window.myLib.initialize();
//Call selectAnimal publicly
//Returns 'Dog'
window.myLib.selectAnimal("d");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment