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.