Question
In the first article, a Boyou raised the following questions. I don't quite understand either. If you have any understanding, can you tell me one or two.
Copy codeThe Code is as follows:
Var str = "test ";
For (var a in str ){
Console. log (a + ":" + str [a]);
}
Output result
This is a string object. When using for, the above situation occurs.
Self-called anonymous function () {}) (window)
Copy codeThe Code is as follows:
(Function (window, undefined ){
// Jquery code
}) (Window );
Code parsing:
The first parentheses: Create an anonymous function.
The second bracket: Execute immediately.
Reason for passing in the window variable:
To change the window variable from a global variable to a local variable, you do not need to roll back the scope chain to the top-level scope to access the window more quickly.
Add the undefined reason to the parameter list:
Make sure that undefined is really undefined within the scope of the self-called anonymous function.
Benefits of this design:
Create a private namespace. The variables and methods in the function body do not affect global space. Does not conflict with other program variables.
Extended extends ()
According to the general design habits, you can directly implement it through the dot (.) syntax, or add an attribute to the prototype object structure. -- The jQuery framework is extended by using the extend () function.
We can also make a similar method. -- Copy all attributes contained in the specified parameter object to the cQuery or cQuery. prototype object.
Copy codeThe Code is as follows:
(Function (){
Var
_ CQuery = window. cQuery,
CQuery = function (){
Return new cQuery. fn. init ();
};
CQuery. fn = cQuery. prototype = {
Init: function (){
Return this;
}
};
CQuery. fn. init. prototype = cQuery. fn;
CQuery. extend = cQuery. fn. extend = function (obj ){
For (var prop in obj ){
This [prop] = obj [prop];
}
Return this;
}
CQuery. fn. extend ({
Test: function (){
Console. log ('test! ');
}
});
Window. C = window. cQuery = cQuery;
})();
// Call Method
C (). test ();
Benefits:
1. It is convenient for users to quickly extend the functions of the jQuery framework without damaging the prototype structure of the jQuery framework.
2. Convenient management.
Note:
Objects extended by prototype must be called by instantiation functions (such as cQuery (). test (), rather than cQuery. test ())
Object url parameterization param ()
Copy codeThe Code is as follows:
(Function (){
Var
_ CQuery = window. cQuery,
CQuery = function (){
Return new cQuery. fn. init ();
};
CQuery. fn = cQuery. prototype = {
Init: function (){
Return this;
}
};
CQuery. param = function (obj ){
Var prefix, s = [];
For (prefix in obj ){
S [s. length] = encodeURIComponent (prefix) + "=" + encodeURIComponent (obj [prefix]);
}
Return s. join ("&");
}
CQuery. fn. init. prototype = cQuery. fn;
Window. C = window. cQuery = cQuery;
})();
Var param = cQuery. param ({"name": "chuanshanjia", "age": 30 });
Console. log (param );
Output result
Object url parameterization: structured and easy to maintain. If you add a parameter list after the url, don't you get dizzy?
Summary
I will write it here for the time being. If you have something to add, it would be better. -- Let's talk about each other and learn from each other.