JQuery 1.10.2 Source Analysis Learning 2

Source: Internet
Author: User
Tags new set

Jquery.fn = Jquery.prototype = {
Jquery:core_version,
Constructor:jquery,
Init:function (selector, context, rootjquery) {
var match, Elem;
if (!selector) {
return this;
}
if (typeof selector = = = = "string") {
if (Selector.charat (0) = = = "<" && Selector.charat (selector.length-1) = = = ">" && selector.length >= 3) {
Match = [NULL, selector, NULL];
} else {
Match = rquickexpr.exec (selector);
}
if (Match && (match[1] | |!context)) {
if (Match[1]) {
Context = Context instanceof JQuery? CONTEXT[0]: Context;
Jquery.merge (This, jquery.parsehtml (
MATCH[1],
Context && Context.nodetype? context.ownerdocument | | Context:document,
True
) );
if (Rsingletag.test (match[1]) && jquery.isplainobject (context)) {
For (match in context) {
if (Jquery.isfunction (this[match])) {
this[Match] (context[match]);
} else {
This.attr (match, context[match]);
}
}
}

return this;
} else {
Elem = document.getElementById (match[2]);
if (Elem && elem.parentnode) {
if (elem.id!== match[2]) {
return Rootjquery.find (selector);
}
This.length = 1;
This[0] = Elem;
}

This.context = document;
This.selector = selector;
return this;
}
} else if (!context | | context.jquery) {
Return (Context | | rootjquery). FIND (selector);
} else {
return This.constructor (context). Find (selector);
}
} else if (Selector.nodetype) {
This.context = this[0] = selector;
This.length = 1;
return this;
} else if (Jquery.isfunction (selector)) {
return Rootjquery.ready (selector);
}

if (selector.selector!== undefined) {
This.selector = Selector.selector;
This.context = Selector.context;
}

return Jquery.makearray (selector, this);
},
Selector: "",
length:0,

Toarray:function () {
Return Core_slice.call (this);
},
Get:function (num) {
return num = = null?
This.toarray ():
(Num < 0 this[this.length + num]: this[num]);
},
Pushstack:function (Elems) {
var ret = Jquery.merge (This.constructor (), elems);
Ret.prevobject = this;
Ret.context = This.context;
return ret;
},
Each:function (callback, args) {
Return Jquery.each (this, callback, args);
},
Ready:function (FN) {
JQuery.ready.promise (). Done (FN);
return this;
},
Slice:function () {
Return This.pushstack (core_slice.apply (this, arguments));
},
First:function () {
Return This.eq (0);
},
Last:function () {
Return This.eq (-1);
},
Eq:function (i) {
var len = this.length,
j = +i + (i < 0? len:0);
Return This.pushstack (J >= 0 && J < len? [This[j]]: []);
},
Map:function (callback) {
Return This.pushstack (Jquery.map (this, function (Elem, i) {
Return Callback.call (Elem, I, elem);
}));
},
End:function () {
return This.prevobject | | This.constructor (NULL);
},
Push:core_push,
Sort: [].sort,
Splice: [].splice
};
JQuery.fn.init.prototype = Jquery.fn;

We first look at the above source code in the last sentence jQuery.fn.init.prototype = Jquery.fn;

The last one we talked about. This is the realization of the JQuery.fn.init Protptype pointed to the JQUERY.FN prototype

This enables the operation of chained programming.

So, let's just look at what this function does inside.

The first is the INIT function.

Selector have a look at the 7 branch situation:

DOM element

Body (optimized)

Strings: HTML tags, html strings, #id, selector expressions

function (as ready callback function)

Finally returns a pseudo-array

ToArray: Restores all DOM elements in the jquery collection to an array

Pushstack: Takes an element from an array, puts it on the heap, and returns the collection of elements on the new heap (jquery object)

End: Most of the jquery traversal methods manipulate a JQuery object instance and generate a new object that matches the set of different DOM elements. When this happens, the new set of elements should be pushed into the stack that is maintained in the object. Each successful filter method call pushes the new element onto the stack. If we need an old set of elements, we can use End () to pop a new collection out of the stack.

Get: Speaking of get needs to put it in conjunction with EQ to return a jquery object (this used pushstack), get returns an array of HTML objects (this uses the ToArray).

Each: Specifies the function to run for each element that is matched to.

Ready: Run the function when the page finishes loading in one by one.

Frist: Selects the first element in a matching element collection.

Last: Selects the final element in the collection of matched elements.

Map: Pass each element through a function to the current matching collection, generating a new JQuery object that contains the return value

The next chapter prepares me to do some detailed explanations and comparisons of the above methods, including their usage

Original: http://blog.csdn.net/zzx252373003/article/details/10911469

JQuery 1.10.2 Source Analysis Learning 2 (EXT)

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.