Implementing Namespaces in JavaScript

Source: Internet
Author: User
Tags copy version
Name space

Note: Haven't written for a long time, today I wrote a copy of the article in the company intranet, out of the show, just turned to web development, so began to learn javascript!

Before introducing namespaces, a problem for developers is how to prevent conflicts between the function name/class name and others. This problem can be solved by naming a book (such as prefix, etc.) between groups within a company, but in the entire area of software development, where today's collaborative development is quite prevalent, The problem is still there. When working with multiple third-party frameworks or libraries, the only thing you can do is to pray for their names not to conflict, and if that happens, the only thing you can do is give up one of them (note: Maybe I am ignorant, hehe). The introduction of namespaces solves this problem to a large extent, of course, if you use the same namespaces as other companies, and the other is Microsoft, Sun and other big guy, then congratulations, hehe @_@!

Engaging in web development will inevitably touch JavaScript, the latest version of JavaScript does not support namespaces, so the problem of naming conflicts is obvious, imagine you cited two JS files, but found that because of naming problems caused you have to give up one of them, This leads to a lot of writing code, which is certainly very frustrating. Before the new version of JavaScript introduces the namespace concept, it is our programmer's basic obligation to promote self-reliance and creativity.

Implementation premise: Unlike Delphi, C # and other languages, the class in JavaScript is not the definition of the object, in fact there is no real class in JavaScript, the class here is actually implemented with function simulation, and the function in JavaScript is actually an object, So in JavaScript: A class is an object. This is very different from the traditional conceptual concept that in JavaScript, creating an instance of a class is actually a copy of the Class (= object, remember). See here, a bit of design pattern concept should be able to see that, in JavaScript, the class mechanism uses the prototype (prototype) pattern.

Implementation principle: Since we see the nature of the class, then the problem is simple, if the GEA project group all JS classes and functions as attributes in the object named Gea, and then the Gea object in the form of attributes in the name of the Grandsoft object can not achieve our purpose, For example, Grandsoft.GEA.Person is actually a class person (or an object) in the Grandsoft object's property GEA (also an object).

Implementation is very simple, the entire namespace mechanism implementation of less than 20 lines of code, analyzed as follows:

Declares a global object namespace, which is used to register namespaces
Namespace = new Object ();

The

//global object only exists in the register function, the parameter is a full path to the namespace, such as "Grandsoft.gea"
Namespace.register = function (fullns)
{
    //Cut the namespaces into n parts, such as Grandsoft, GEA,
    var nsarray = Fullns.split ('. ');
    var seval = "";
    var SNS = "";
    for (var i = 0; i < nsarray.length i++)
    {
    & nbsp;   if (i!= 0) SNS = ".";
        SNS = nsarray[i];
       //To create a statement that constructs a Namespace object (if it does not exist)
         For example, create the Grandsoft first, then create the Grandsoft.gea, in turn,
        seval = "If" ( typeof ("+ SNS +") = = ' undefined ') "+ SNS +" = new Object (); "
   }
    if (seval!= "") eval (seval);
}
above is the complete implementation of simulating the namespace mechanism in JavaScript, using the following methods:


Register namespace Grandsoft.gea, GRANDSOFT.GCM
Namespace.register ("Grandsoft.gea");
Namespace.register ("GRANDSOFT.GCM");

Declaring class person in Grandsoft.gea namespaces
Grandsoft.GEA.Person = function (name, age)
{
THIS.name = name;
This.age = age;
}

Add a public method to the class person show ()
Grandsoft.GEA.Person.prototype.show = function ()
{
Alert (THIS.name + "is" + This.age + "Years old!");
}

Demonstrates how to use the class person
var p = new Grandsoft.GEA.Person ("YANGLF", 25);
P.show ();
Haha, simple, such a simple code I do not say more, we look at play it, in fact, I am a little lazy, hehe @_@!!!

Http://www.cnblogs.com/leafyoung/archive/2006/10/11/526570.html



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.