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
You blog provides a workaround for creating namespaces in JavaScript using objects. While JavaScript doesn't have native support for namespaces, the example demonstrates how objects can be utilized to mimic namespace-like behavior. By nesting objects, you can organize your code and create a hierarchical structure similar to namespaces.
ReplyDeleteTo know more about in detail then Java Certification Course in Lucknow would be better option.