Extends of jquery:
The Extension Method extend of jquery is a commonly used method in the process of writing plug-ins. This method has some heavy-load prototypes. Here, let's get to know about it.
1. The extension method prototype of jquery is:
Extend (DEST, src1, src2, src3 ...);
It means to merge src1, src2, src3... into the dest, and the returned value is the merged DeST. It can be seen that the structure of the Dest is modified after the method is merged. If you want to get the merged result but do not want to modify the Dest structure, you can use the following:
VaR newsrc = $. Extend ({}, src1, src2, src3 ...) // That is, "{}" is used as the Dest parameter.
In this way, you can merge src1, src2, src3... and return the merged result to newsrc. For example:
VaR result = $. Extend ({}, {Name: " Tom " , Age: 21 }, {Name: " Jerry " , Sex: " Boy " })
Then the merged result
Result = {Name: " Jerry " , Age: 21 , Sex: " Boy " }
That is to say, if the following parameter has the same name as the preceding parameter, the following parameter will overwrite the preceding parameter value.
Ii. DEST parameter omitted
The Dest parameter in the above extend method prototype can be omitted. If it is omitted, the method can only have one SRC parameter, in addition, the SRC is merged into the object that calls the extend method, for example:
1. $. Extend (SRC)
This method combines SRC into jquery's global object, for example:
$. Extend ({
Hello: function () {alert ( ' Hello ' );}
}); Copy code
Is to merge the hello method into the global object of jquery.
2. $. FN. Extend (SRC)
This method combines SRC into jquery's instance object, for example:
$. FN. Extend ({
Hello: function () {alert ( ' Hello ' );}
});
Copy code
Is to merge the hello method into the jquery instance object.
Below are some examples of common extension instances:
$. Extend ({net :{}});
This is to expand a net namespace in the jquery global object.
$. Extend ($. net ,{
Hello: function () {alert ( ' Hello ' );}
}) Copy code
This is to extend the hello Method to the expanded jquery net namespace.
III. The extend method of jquery also has an overload prototype:
Extend (Boolean, DEST, src1, src2, src3 ...)
The first parameter 'boolean' indicates whether to perform a deep copy. The other parameters are the same as described above. What is "Deep copy"? Let's look at an example:
VaR result = $. Extend ( True ,{},
{Name: " John " , Location: {city: " Boston " , County: " USA " }},
{Last: " Resig " , Location: {state: " Ma " , County: " China " }}); Copy code
We can see that the child Object Location: {City: "Boston"} is embedded in src1, and the sub-Object Location: {state: "Ma"} is also nested in src2. The first parameter for deep copy is true, the merged result is:
Result = {Name: " John " , Last: " Resig " ,
Location: {city: " Boston " , State: " Ma " , County: " China " }}
Copy code
That is to say, it will merge nested sub-objects in SRC. If the first parameter Boolean is false, let's look at what the merging result is, as shown below:
VaR result = $. Extend ( False ,{},
{Name: " John " , Location: {city: " Boston " , County: " USA " }},
{Last: " Resig " , Location: {state: " Ma " , County: " China " }}
); Copy code
The merged result is:
Result = {Name: " John " , Last: " Resig " , Location: {state: " Ma " , County: " China " }}
The above are some of the details that $. Extend () often uses in projects.
Extends of jquery:
The Extension Method extend of jquery is a commonly used method in the process of writing plug-ins. This method has some heavy-load prototypes. Here, let's get to know about it.
1. The extension method prototype of jquery is:
Extend (DEST, src1, src2, src3 ...);
It means to merge src1, src2, src3... into the dest, and the returned value is the merged DeST. It can be seen that the structure of the Dest is modified after the method is merged. If you want to get the merged result but do not want to modify the Dest structure, you can use the following:
VaR newsrc = $. Extend ({}, src1, src2, src3 ...) // That is, "{}" is used as the Dest parameter.
In this way, you can merge src1, src2, src3... and return the merged result to newsrc. For example:
VaR result = $. Extend ({}, {Name: " Tom " , Age: 21 }, {Name: " Jerry " , Sex: " Boy " })
Then the merged result
Result = {Name: " Jerry " , Age: 21 , Sex: " Boy " }
That is to say, if the following parameter has the same name as the preceding parameter, the following parameter will overwrite the preceding parameter value.
Ii. DEST parameter omitted
The Dest parameter in the above extend method prototype can be omitted. If it is omitted, the method can only have one SRC parameter, in addition, the SRC is merged into the object that calls the extend method, for example:
1. $. Extend (SRC)
This method combines SRC into jquery's global object, for example:
$. Extend ({
Hello: function () {alert ( ' Hello ' );}
}); Copy code
Is to merge the hello method into the global object of jquery.
2. $. FN. Extend (SRC)
This method combines SRC into jquery's instance object, for example:
$. FN. Extend ({
Hello: function () {alert ( ' Hello ' );}
});
Copy code
Is to merge the hello method into the jquery instance object.
Below are some examples of common extension instances:
$. Extend ({net :{}});
This is to expand a net namespace in the jquery global object.
$. Extend ($. net ,{
Hello: function () {alert ( ' Hello ' );}
}) Copy code
This is to extend the hello Method to the expanded jquery net namespace.
III. The extend method of jquery also has an overload prototype:
Extend (Boolean, DEST, src1, src2, src3 ...)
The first parameter 'boolean' indicates whether to perform a deep copy. The other parameters are the same as described above. What is "Deep copy"? Let's look at an example:
VaR result = $. Extend ( True ,{},
{Name: " John " , Location: {city: " Boston " , County: " USA " }},
{Last: " Resig " , Location: {state: " Ma " , County: " China " }}); Copy code
We can see that the child Object Location: {City: "Boston"} is embedded in src1, and the sub-Object Location: {state: "Ma"} is also nested in src2. The first parameter for deep copy is true, the merged result is:
Result = {Name: " John " , Last: " Resig " ,
Location: {city: " Boston " , State: " Ma " , County: " China " }}
Copy code
That is to say, it will merge nested sub-objects in SRC. If the first parameter Boolean is false, let's look at what the merging result is, as shown below:
VaR result = $. Extend ( False ,{},
{Name: " John " , Location: {city: " Boston " , County: " USA " }},
{Last: " Resig " , Location: {state: " Ma " , County: " China " }}
); Copy code
The merged result is:
Result = {Name: " John " , Last: " Resig " , Location: {state: " Ma " , County: " China " }}
The above are some of the details that $. Extend () often uses in projects.