Skip to content

Instantly share code, notes, and snippets.

@csdear
Created January 6, 2015 12:18
Show Gist options
  • Save csdear/22d65d245819680902cc to your computer and use it in GitHub Desktop.
Save csdear/22d65d245819680902cc to your computer and use it in GitHub Desktop.
Creating a Namespace Using an IFEE □ (Immediate Function Execution Expression -- run immediately, run once etc. ) □ Adds robustness to the ns with this IFEE language feature. □ Use IFEEs to initialize a namespace once with its relevent functionatily. Wrap the code in an IFEE
var ns;
(function (ns) {
ns.Car = function (type) {
this.speed = 0;
this.type = type || "No Type";
}
ns.Car.prototype = {
drive: function(newSpeed) {
this.speed = newSpeed;
}
}
}(ns = ns ||{}));
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment