jquery Source Analysis---expand (extended)

Source: Internet
Author: User

9 Architecture of jquery

9.1, Architecture Overview

The first two articles on the jquery source are analyzed one by one. So we are now standing at a certain altitude to analyze or look at jquery's source code.

jquery's source code differs from that of prototype,mootools, and they extend a lot of array,string,event,hash, and then provide a convenient and compatible operation for DOM elements such as Element,form.

Jqueryr source code is also different from Yui, the use of components in accordance with the Java object-oriented rules in the law to build class library, to provide users with convenient operation.

jquery is just an object, an object of an array of classes. It does not extend the original object of JS. It does not have a hierarchical hierarchy to form such an inheritance way (this is the case in ext). It simply blends what it thinks it needs into the object of jquery. For the simplicity of the code, it uses the style of method chain programming (originated from Hibernate).

From the code's point of view, the jquery code is divided into two parts, one is the instance method, the most common set of instances constructed by $ (). The second part is the static method. Accessed through the namespace variables of jquery. such as Jquery.attr (). It generally accomplishes specific tasks in static methods. Then, if the instance method needs to use it for proxy.

9.2, Extend

Someone criticized (as if it were fins) prototype, saying it was a extend supporting half the sky. For jquery, it is also a extend support side of the day. jquery provides the extend to extend the jquery. JQuery.fn.extend is an extension of the jquery object. Jquery.extend is an extension of the static method of jquery. They are the same method.

Jquery.extend = JQuery.fn.extend = function () {

var target = Arguments[0] | | {},//The first parameter is the target
i = 1, length = arguments.length, deep = false, options;

if (Target.constructor = = Boolean) {//The first parameter is bool type
Deep = target;//Depth Copy
target = Arguments[1] | | {};//target points to the second parameter
i = 2;
}

Target is of type string or?
if (typeof target!= "Object" && typeof target!= "function")
target = {};

if (length = = i) {///only one parameter? Or deep copy, two parameters
target = this;//destination is this
I.;
}

for (; i < length; i++)
if (options = arguments[i])!= null)

for (var name in options) {
var src = target[name], copy = Options[name];
if (target = = copy)//Prevent Dead loop
Continue
Deep copy processing, most deeply elements
if (deep && copy && typeof copy = = "Object" &&! Copy.nodetype)
Target[name] = jquery.extend (deep, SRC
|| (Copy.length!= null?) []: {}), copy);
else if (copy!== undefined)//Direct copy
Target[name] = copy;

}

return target;
};

Jquery.extend How to say also than prototype1.4 extend strong. It not only supports deep clone, but also supports multiple parameters of object clone to a specified object instead of jquery. Compared to Ext's inheritance, I feel that this kind of inheritance in jquery is enough.

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.