Learn JavaScript (iii) _jquery through the jquery source code

Source: Internet
Author: User
Tags extend
Questions

The first part of a Bo friends raised the following questions, I do not quite understand, if there is clear, whether to inform one or two.

Copy Code code as follows:

var str = "Test";
For (var a in str) {
Console.log (A + ":" + str[A]);
}

Output results
This is a string object that appears above when you use a for.

Self-Invoke anonymous function (function () {}) (window)
Copy Code code as follows:

(Function (window, undefined) {
jquery Code
}) (window);

Code resolution:

First parenthesis: Create an anonymous function.
Second bracket: immediate execution.

Incoming window variable reason:
Make window variables local by global variables, and you do not need to return the scope chain to the top-level scope for faster access to Windows.
To increase the undefined reason in the argument list:
In the scope of the call anonymous function, make sure that the undefined is really undefined.
The benefits of this design are:
Create a private namespace. The variables and methods in the body of the function do not affect the global space. Does not conflict with variables of other programs.

feature extension Extend ()

According to the general design habits, you can directly through the point (.) Syntax implementation, or add a property to the prototype object structure. The--jquery framework is implemented through the Extend () function to extend the function.
Let's do a similar approach. --Copies all the attributes contained in the specified Parameter object to the Cquery or Cquery.prototype object.
Copy Code code 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 mode
C (). Test ();

Benefits:
1, user-friendly fast expansion of the jquery framework function, will not destroy the jquery framework prototype structure.
2, convenient management.
Attention:
By prototype the extended object, we must invoke it by instantiating the function (such as cquery (). Test () instead of the Cquery.test ())

object URL parameterized param ()
Copy Code code 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 results

Object URL parameterization: It is advantageous to the structure, easy to maintain. If you add a lump of argument list after the URL, don't you look Ching?

Summary

To write here for the time being, if you have something to add, that's best. --We have more exchanges and learn from each other.
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.