IIFE
IIFE
by isogunro
JavaScript
//To prevent pollution of the global namespace, create an empty object and assign it to a variable. you can then add members to it.
function vehicle() {
alert("HELLO VEHCILE");
}
(function () {
this.myApp = {} || myApp; //Creates the namespace object if it does not already exist
var ns = this.myApp;
alert("Should see this since it's a IIFE");
var vehicleCount = 5;
var vehicles = [];
ns.Car = function () {};
ns.Truck = function () {};
var repair = {
decription: 'changed spark plugs',
cost: 100
};
alert("Problem: " + repair.description + " Cost: " + repair.cost);
}());