Implement namespace in JavaScript.

Source: Internet
Author: User
(Function () {var namespacearray = []; function namespaceobject (name, fullname) {This. fullname = fullname; this. name = Name ;}; var namespacemanager ={ namespaces: namespacearray, Global: This}; function using (namespace, Action) {If (! Namespace) throw new error ('namespace cannot be blank. '); Else if (typeof (namespace )! = "String") throw new error ('namespace can only be a string. '); Else if (namespace. charat (0) = '. '| namespace. charat (namespace. length-1) = '. '| namespace. indexof (".. ")! =-1) {Throw new error ("invalid namespace:" + namespace + ". ");} Var parentns = namespacearray [namespace]; If (! Parentns) {var NSS = namespace. split ('. '); //-split namespace var parentns = namespacemanager. global; //-assume that the upper level is the global namespace var fullname; For (VAR I = 0; I <NSS. length; I ++) {var NS = NSS [I]; If (! Fullname) fullname = ns; else fullname = fullname + "." + NS; var nsobject = parentns [ns]; If (! Nsobject) {nsobject = new namespaceobject (name, fullname); parentns [ns] = nsobject;} else if (typeof nsobject! = "Object") throw new error (NSS. Slice (0, I). Join ('.') + "non-object namespace. "); Else nsobject = parentns [ns]; parentns = nsobject;} namespacearray [namespace] = parentns;} If (typeof action =" function ") {action. call (parentns);} If (parentns! = Namespacemanager. Global) {for (var I in parentns) {If (! Namespacemanager. global [I]) namespacemanager. global [I] = parentns [I] ;}} return parentns ;}; function clearusing (namespace) {var parentns = namespacearray [namespace]; If (parentns) {for (var I in parentns) {if (I = "name" | I = "fullname" | typeof parentns [I] = "namespaceobject") continue; if (namespacemanager. global [I]) {Delete namespacemanager. global [I] ;}}} window. using = using; wi Ndow. clearusing = clearusing; window. namespacemanager = namespacemanager; using ("sofire. test ", function () {This. helloclass = function () {This. name = "hello" ;};}); alert (helloclass); clearusing ("sofire. test "); If (typeof helloclass! = "Undefined") {alert ("exits");} else alert ("not exits"); Using ("sofire. test ", function () {This. helloclass2 = function () {This. name2 = "hello2" ;}}); alert (helloclass); alert (helloclass2 );})();
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.