Example 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ //opens the closure | |
//definition of private object Animal | |
var Animal= function(){ | |
this.name=""; | |
}; | |
Animal.prototype={ | |
getAnimal : function(code){ | |
var s = this; | |
s.getAnimalCallBackFn = function(response){ | |
this.name=response; | |
}; | |
//AJAX call to get the description | |
$.ajax({ | |
url : "getDescription.json", | |
dataType : "json", | |
data : JSON.stringify({"code" : code}), | |
success : s.getAnimalCallBackFn | |
}); | |
} | |
}; | |
//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