$. Extend () usage summary.
JQuery provides two methods for development plug-ins:
JQuery. fn. extend (object );
JQuery. extend (object );
JQuery. extend (object); adds a new method to the class to extend the jQuery class itself.
JQuery. fn. extend (object); add a method to the jQuery object. This should be easy to understand. For example.
new soulnew soulnew soulnew soul<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"}});</script>
Okay. You also saw a little usage of $. extend.
1. Merge multiple objects.
The nested object function of $. extend () is used here.
Nesting multiple objects is similar to merging arrays.
But here is the object. Examples.
// 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 attribute of Css1 is overwritten and inherits the weight attribute of Css2 // Css1 = {size: "12px", style: "oblique", weight: "bolder "}
2. Deep nested object.
JQuery. extend ({name: "John", location: {city: "Boston" },{ last: "Resig", location: {state: "MA "}}); // result: // =>{ name: "John", last: "Resig", location: {state: "MA" }}// new more in-depth. extend () jQuery. extend (true, {name: "John", location: {city: "Boston" }}, {last: "Resig", location: {state: "MA"}); // result // => {name: "John", last: "Resig", // location: {city: "Boston", state: "MA "}}
3. You can add a static method to jQuery.
<script type="text/javascript" src="jquery.2.0.3.js"></script><script type="text/javascript">$.extend({add:function(a,b){return a+b;},minus:function(a,b){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);</script>
Best Wishes