JQuery $. extend () Usage

Source: Internet
Author: User

$. 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

Related Article

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.