stuart-d2
1/6/2015 - 12:17 PM

Creating a Namespace. Here, we are adding a object constructor "Car" to a namespace.

Creating a Namespace. Here, we are adding a object constructor "Car" to a namespace.

 var ns = ns || {};
ns.Car = function (type) {
	this.speed= 0;
	this.type = type || "No Type";
	}
ns.Car.prototype = { 
	drive: function(newSpeed) {
		this.speed = newSpeed;
		}
	}

var bmw = new ns.Car("BMW");
console.log(bmw.speed); // outputs 0
console.log(bmw.type); // outputs "BMW"
bmw.drive(80);
console.log(bmw.speed); // outputs 80