jQuery-1.9.1 Source Analysis Series (i) overall structure continued

Source: Internet
Author: User

This section is primarily a few of the most basic things in jquery

2. Several basic properties and functions of jqueryA. jquery.noconflict function

Save external $ and jquery when jquery is initialized

  _jquery = Window.jquery,

_$ = window.$,

noconflict function

noconflict: function (deep) {

if (window.$ = = = JQuery) {

window.$ = _$;

}

if (deep && window.jquery = = = JQuery) {

Window.jquery = _jquery;

}

return jQuery;

}

Call Noconflict to let go of the use of $ even jquery. The returned jquery is saved as a custom variable. Such as

var myjq = $.noconflict ();

Then you can use MYJQ as a jquery.

var PS = myjq ("P");//Gets the collection of elements for all P tags.

  

b. jquery.extend = JQuery.fn.extend function

Users may need jquery and Jquery.prototype (Jquery.fn/jquery (...) when they use jquery again. To expand (Add properties or methods), this time use to extend.

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

Jquery.extend is the expansion of the jquery itself, JQuery.fn.extend is the expansion of the Jquery.fn, that is, the expansion of the Jquery.prototype, the final performance of the jquery instance $ (...) The expansion.

Source code Analysis: The source code is relatively simple, here do not do analysis, but in which there is a technical point

When using extend, it is important to note that, according to the principle that the JS Rule object variable has only one copy, if a property in a shallow copy is a value obtained through an object variable , the copy result will change if the object variable is changed externally.

eg

var hd = {name: ' Hard '};

var pc = {soft: ' soft ', Hdwe:HD};

var tt = {td: ' Test '};

var val = $.extend (TT,PC);//{td: "Test", Soft: "soft", Hdwe: {name: ' Hard '}}

  HD. Name = ' Chenhua ';

The value of Val at this time is {td: "Test", Soft: "soft", Hdwe: {name: ' Chenhua '}}

C. jquery.type function to identify object types

JavaScript also comes with a typeof operator that can determine the type of data. However, for most objects, the typeof operator returns "Object" and cannot distinguish between specific types. jQuery.type()the type of JS built-in object can be determined more precisely.

Class2type = {},

core_tostring = class2type.tostring,

Type:function (obj) {

if (obj = = null) {

return String (obj);

}

return typeof obj = = = "Object" | | typeof obj = = = "function"?

class2type[Core_tostring.call (obj)] | | "Object":

typeof obj;

}

  

Jquery.each ("Boolean number String function Array Date RegExp Object Error". Split (""), Function (i, name) {
class2type["[Object" + name + "]"] = name. toLowerCase ();
});

Code Core_tostring.call (obj) uses the object's toString to manipulate obj to get the type of JS built-in object, which results in something like "[Object Object]", "[Object Function]", "[Object Array] "style, and finally get the full lowercase form of the type of JS built-in object.

  

Jquery.Type( Undefined ); "Undefined"
Jquery.Type( Null ); "NULL"

Jquery.Type( True ); "Boolean"
Jquery.Type( New Boolean(True) ); "Boolean"

Jquery.Type( 3 ); "Number"
Jquery.Type( New Number(3) ); "Number"

Jquery.Type( "Test" ); "String"
Jquery.Type( New String("Test") ); "String"

Jquery.Type( function(){} ); "Function"
Jquery.Type( New Function() ); "Function"

Jquery.Type( [] ); "Array"
Jquery.Type( New Array() ); "Array"

Jquery.Type( New Date() ); "Date"

Jquery.Type( New Error() ); "Error"//JQuery 1.9 added support

Jquery.Type( /test/ ); "RegExp"
Jquery.Type( New Regexp("\\d+") ); "RegExp"

/* except for objects of the type above, all other objects Return "object" */

jquery< Span class= "pun". type ( {} ); //"object"
function user () {}
Jquery. ( new user () //"object"

d. Jquery.function and JQuery.fn.function

Some functions are directly bound to jquery, and this method can only be invoked using Jquery.function (). And the method is bound to Jquery.fn, this method generally has two ways of calling jQuery.fn.function () or jquery (...). function

According to the jquery initialization function jquery = function (Selector,context) {

return new JQuery.fn.init (selector,context,rootjquery);

} should be easy to understand. JQuery (...) The resulting context is jquery.fn, so a function bound to Jquery.fn can ultimately be invoked through jquery (Selector,context). function ().

jQuery-1.9.1 Source Analysis Series (i) overall structure continued

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.