var com = {}; com.google = {}; com.google.www = {}; com.google.www.search = function(s) { // since we're actually working in the context of an object, // we can refer to "this" to set static-ish variables, like a search history: if (!this.search_history) { this.search_history = []; } this.search_history.push(s); // perform the actual "search" ... alert("No results for " + s + "! Try searching with svidgen.com instead!"); } // com.google.www.search() // you can then use "with" // even though you shouldn't generally be using "with" ... with (com.google.www) { search("for something"); search("for something else"); } // we should be able to see the search history in the // namespace now alert(com.google.www.search_history.length);
And there you have it. Namespaces in JavaScript.
Wow. This really made my day. Thanks a lot!
ReplyDeleteJavascript Training in Chennai | HTML5 Online Training
JavaScript Training Courses | Javascript Online Training | Angular 2 Training in Chennai
JavaScript Online Training JavaScript Online Training JQuery Online Training JQuery Online Training
ReplyDeleteJavaScript Course | HTML5 Online Training
HR Apps
ReplyDeleteManufacturing Apps