Simple analysis of namespace function based on javascript-basic knowledge

Source: Internet
Author: User
Before see NetEase or Sina or what what website again, also use similar this kind of thing, at that time to JS just bucket level, on the spot was scared silly.
But this time, I intend to personally analyze, later in see will not be confused.
Copy Code code as follows:

Call Example: Bradio.namespace ("Bradio.lang.array");
The following is the source code after the compilation of Baidu, variables are a,b,c,d ... And then I'll write a code point again.
All right, start the analysis.
Bradio.namespace = function () {//Create namespace
var a = arguments,//["Bradio.lang.array"]
b = null,//for storing the top level object
C, D, E, F; C is the counter, D is the counter of the inner loop, E is a split namespace array, F is the arguments parameter length
c = 0;
for (f = a.length; C < F; C + +) {
E = ("" + A[c]). Split ("."); Split namespace, split ["Bradio", "Lang", "array"]
b = Bradio; Base object for adding namespaces based on this object
for (d = "bradio" = = E[0]? 1:0; d < e.length; d++)//If the first is Bradio, the second begins traversal ["Bradio", "Lang", "array"]
B[e[d]] = b[e[d]] | | {}, B = B[e[d]]
actually equals
B[e[d]] = b[e[d]] | | {}///If the current object has already been manipulated with the current object, the assignment null object does not exist
b = b[e[d]]//overwrite B with Word object, Next loop add namespace based on child object
}
Return b//returns the last level of the object
};

The following is a sorted, read-only version
Copy Code code as follows:

Bradio.namespace ("Bradio.lang.array");
Bradio.namespace = function () {//Create namespace
var args = arguments,
Parent = NULL,
Arr, I, J, Len;
For (i=0, len = Args.length i < len; i++) {
arr = ("" + Args[i]). Split (".");
parent = Bradio;
for (j = "Bradio" = = Arr[0]? 1:0 J < Arr.length J + +) {
parent[Arr[j]] = parent[Arr[j]] | | {};
Parent = parent[Arr[j]];
}
}
return to parent;
}

Of course, only my understanding, if where is not very right, but also ask the heroes to help point out, so as not to mislead everyone.
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.