jQuery.extend() and jQuery.fn.extend() are the same… Right?

This question already has an answer here:

  • Difference between jQuery.extend and jQuery.fn.extend? 5 answers

  • $.extend simply extends an object

    var obj1 = {'name' : 'Progo'};
    var obj2 = {'value' : 'stack overflow'};
    
    $.extend(obj1, obj2);
    
    // obj1 is now {'name' : 'Progo', 'value' : 'stack overflow'}
    

    FIDDLE

    jQuery.fn.extend extends the jQuery prototype

    jQuery.fn.extend({
      turn_red: function() {
        return this.each(function() {
          this.style.color = 'red'
        });
      }
    });
    
    // gives you
    
    $('elements').turn_red(); // sets color to red
    

    FIDDLE


    The key difference is here:

    // extend jQuery itself if only one argument is passed
    if ( length === i ) {
        target = this;
        --i;
    }
    

    this will be different depending on whether $.extend or $.fn.extend was called.

    链接地址: http://www.djcxy.com/p/14214.html

    上一篇: .Net将对象序列化为自定义JSON(排序)格式

    下一篇: jQuery.extend()和jQuery.fn.extend()是一样的...对吗?