Implementing namespace

To prevent pollution of the global namespace, create an empty object and assign it to a variable. you can then add members to it.

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.
var myApp = myApp || {}; //Creates the namespace object if it does not already exist
myApp.vehicleCount = 5;
myApp.vehicles = new Array();
myApp.Car = function() { }
myApp.Truck = function() {}

myApp.repair = {
    decription: 'changed spark plugs',
    cost: 100
};