Js simple namespace Manager

Source: Internet
Author: User

Function $ package (name)
{
// Split the namespace domain string
Var domains = name. split (".");
Var cur_domain = window;
// Cyclically traverse each subdomain
For (var I = 0; I <domains. length; I ++)
{
Var domain = domains [I];
// If the space of this domain is not created
If (typeof (cur_domain [domain]) = "undefined ")
{
// Create a domain
Cur_domain [domain] = {};
}
// Set the current domain as the domain of this loop
Cur_domain = cur_domain [domain];
}
Return cur_domain;
};

// Use the with reference method
$ Package ("com. anllin. system ");
$ Package ("com. hs. test ");
With (com. anllin)
With (com. hs)
{
Alert ("system1:" + system );
Alert ("test1:" + test );
}

// Use the closure Reference Method
$ Package ("com. anllin. system ");
$ Package ("com. hs. test ");
(Function (){
Var system = com. anllin. system;
Var test = com. hs. test;
Alert ("system2:" + system );
Alert ("test2" + test );
})();


Source of Water

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.