Simulation code of jquery principles-4 important extended functions extend

Source: Internet
Author: User

Last two articlesArticle, We can see that every time we want to add a sharing method through the jquery prototype, we need to use jquery. it is very troublesome to extend FN one by one, jquery. FN. extend provides an extension mechanism to extend an object through one or more sample objects. If no extended object is specified, it will be extended to itself.

 

Jquery. ExtendIt can also be used through jquery. FN. Extend. There are many extensions used in jquery for a target object. The extension members come from a series of reference objects.

 

In this way, if we need to extend the removedata member for jquery. FN, we can do this.

Jquery. FN. Extend (
{
Removedata: Function (Key ){
Return   This . Each ( Function (){
Jquery. removedata ( This , Key );
});
}
}
);

 

The source code of extend is as follows, because it is relatively simple, so there is not much streamlining.

 

1 // /<Reference Path = "jQuery-core.js"/>
2
3
4 Jquery. Extend = Jquery. FN. Extend =   Function (){
5 // Copy reference to target object
6 VaR Target = Arguments [ 0 ] | {}, I =   1 , Length = Arguments. length, deep =   False , Options, name, SRC, copy;
7
8 // In the case of deep copy, the first parameter is of the boolean type, which indicates deep copy. The second parameter is the target object.
9 If ( Typeof Target ===   " Boolean " ){
10 Deep = Target;
11 Target = Arguments [ 1 ] | {};
12 // Skip the Boolean and the target
13 I =   2 ;
14 }
15
16 // If the target is neither an object nor a function
17 If ( Typeof Target ! =   " Object "   &&   ! Jquery. isfunction (target )){
18 Target = {};
19 }
20
21 // If only one parameter is added
22 If (Length === I ){
23 Target =   This ;
24 -- I;
25 }
26
27 // Traverse all reference objects and extend them to the target object.
28 For (; I < Length; I ++ ){
29 // Only deal with non-null/undefined values
30 If (Options = Arguments [I]) ! =   Null ){
31 // Extend the Base Object
32 For (Name In Options ){
33 SRC = Target [name];
34 Copy = Options [name];
35
36 // Prevent never-ending loop
37 If (Target === Copy ){
38 Continue ;
39 }
40
41 // Recurse if we're re merging object literal values or Arrays
42 If (Deep && Copy && (Jquery. isplainobject (copy) | Jquery. isarray (copy ))){
43 VaR Clone = SRC && (Jquery. isplainobject (SRC) | Jquery. isarray (SRC )) ? SRC
44 : Jquery. isarray (copy) ? []: {};
45
46 // Never move original objects, clone them
47 Target [name] = Jquery. Extend (deep, clone, copy );
48
49 // Don't bring in undefined values
50 } Else   If (Copy ! = Undefined ){
51 Target [name] = Copy;
52 }
53 }
54 }
55 }
56
57 // Return the modified object
58 Return Target;
59 };

 

Simulation of jquery principlesCode-0 directory

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.