Just started out learning js and using the book Javascirpt an Absolute Beginner's Guide. Question is from an example in the book:
var awesomeSauce = (
function () {
var secretCode = "Zorb!";
function privateCheckCode(code) {
if (secretCode == code) {
alert("You are awesome!");
} else {
alert("Try again!");
}
}
// the public method we want to return
return {
checkCode: privateCheckCode
};
})();
Question is how do I go about to call this code?
awesomeSauce("Zorg!");
doesnt work and neither does
awesomeSauce().privateCheckCode("Zorg!");
The IIFE returns an object with the
checkCode
property, that property is the (private) function.The point of the IIFE is that this scopes the variables and functions within, so that they are not accessible from outside (e.g.
privateCheckCode
andsecretCode
exist only inside the IIFE).Think of the returned object as an "export" of selected values or functionality.