jquery has two methods for developing plug-ins, namely:
JQuery.fn.extend (object);
Jquery.extend (object);
Jquery.extend (object); To extend the jquery class itself. Adds a new method to the class.
JQuery.fn.extend (object); Adds a method to a jquery object.
As an example,
[HTML]View PlainCopy
- <h3 class="Ye">new soul</h3>
- <h3 class="Ye">new soul</h3>
- <h3 class="Ye">new soul</h3>
- <h3 class="Ye">new soul</h3>
- <script type="Text/javascript" src="Jquery.2.0.3.js"></Script >
- <script type="Text/javascript">
- JQuery.fn.myPlugin = function (options) {
- $options = $.extend ({
- HTML: "No Messages",
- CSS: {
- "Color": "Red",
- "Font-size": "14px"
- }},
- Options);
- return $ (this). CSS ({
- "Color": $options. Css.color,
- }). html ($options. html);
- }
- $ ('. Ye '). Myplugin ({html: "So easy,yes?", css:{"color": "Green", "font-size": "20px"});
OK, so you see a little bit of $.extend () usage.
1. Merge multiple objects.
This is where $.extend () is used to nest multiple objects.
The so-called nested multiple objects, a bit similar to the merging of arrays of operations.
But here is the object. To illustrate:
[JavaScript]View PlainCopy
- Usage: jquery.extend (obj1,obj2,obj3,..)
- var css1={size: "10px", Style: "Oblique"}
- var css2={size: "12px", Style: "oblique", Weight: "bolder"}
- $.jquery.extend (CSS1,CSS2)
- Result: The Size property of the Css1 is overwritten, and the weight property of the Css2 is inherited
- Css1 = {size: "12px", Style: "Oblique", Weight: "Bolder"}
2. Deep nested objects.
[JavaScript]View PlainCopy
- Jquery.extend (
- {Name: "John", Location: {City: "Boston"}},
- {Last: ' Resig ', location: ' {state: ' MA '}}
- );
- Results:
- = = {Name: ' John ', Last: ' Resig ', location: ' {state: ' MA '}}
[JavaScript]View PlainCopy
- New more in-depth. Extend ()
- Jquery.extend ( True,
- {Name: "John", Location: {City: "Boston"}},
- {Last: ' Resig ', location: ' {state: ' MA '}}
- );
- Results
- = = {Name: "John", Last: "Resig",
- Location: {city: ' Boston ', State: ' MA '}}
3. You can add static methods to jquery
[JavaScript]View PlainCopy
- $.extend ({
- Add:function (A, b) {return a+b;},
- Minus:function (A, A, (a) {return a-B},
- Multiply:function (A, b) {return a*b;},
- Divide:function (A, b) {return Math.floor (A/b);}
- });
- var sum = $.add (3,5) +$.minus (3,5) +$.multiply (3,5) +$.divide (5,7);
- Console.log (sum);
JQuery $.extend () Usage Summary