【问题标题】:Is There any Performance Difference in the Ways of Writing a jQuery Plugin?编写 jQuery 插件的方式有性能差异吗?
【发布时间】:2011-01-09 20:39:00
【问题描述】:

我很好奇,编写 jQuery 插件(如果有的话)的方式有哪些(性能)差异?

我已经看到了几种方法:

1.使用$.extend()

(function($){
  $.fn.extend({
    newPlugin: function(){
      return this.each(function(){

      });
    }
  });
})(jQuery);

2。您自己的功能:

(function($){
  $.fn.newPlugin = function(){
      return this.each(function(){

      });
  }
})(jQuery);

恕我直言,第二种方式更简洁,更易于使用,但使用$.extend() 编写它似乎有一些优势?还是我想多了,没有明显的区别,只是个人喜好问题?

(我原以为以前会问过这个问题,但我找不到——如果它在那里,请引导我找到它)

【问题讨论】:

    标签: jquery performance


    【解决方案1】:

    好吧,考虑一下当您执行$.extend 时,您正在调用此方法:

    jQuery.extend = jQuery.fn.extend = function() {
        // copy reference to target object
        var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options, name, src, copy;
    
        // Handle a deep copy situation
        if ( typeof target === "boolean" ) {
            deep = target;
            target = arguments[1] || {};
            // skip the boolean and the target
            i = 2;
        }
    
        // Handle case when target is a string or something (possible in deep copy)
        if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
            target = {};
        }
    
        // extend jQuery itself if only one argument is passed
        if ( length === i ) {
            target = this;
            --i;
        }
    
        for ( ; i < length; i++ ) {
            // Only deal with non-null/undefined values
            if ( (options = arguments[ i ]) != null ) {
                // Extend the base object
                for ( name in options ) {
                    src = target[ name ];
                    copy = options[ name ];
    
                    // Prevent never-ending loop
                    if ( target === copy ) {
                        continue;
                    }
    
                    // Recurse if we're merging object literal values or arrays
                    if ( deep && copy && ( jQuery.isPlainObject(copy) || jQuery.isArray(copy) ) ) {
                        var clone = src && ( jQuery.isPlainObject(src) || jQuery.isArray(src) ) ? src
                            : jQuery.isArray(copy) ? [] : {};
    
                        // Never move original objects, clone them
                        target[ name ] = jQuery.extend( deep, clone, copy );
    
                    // Don't bring in undefined values
                    } else if ( copy !== undefined ) {
                        target[ name ] = copy;
                    }
                }
            }
        }
    
        // Return the modified object
        return target;
    };
    

    但是当您调用$.fn.newPlugin 时,您只是在jQuery 的原型对象上附加(或可能覆盖)数据,这与$.fn.extend 所做的几乎完全相同...

    似乎很容易解释哪种方法的开销较小(后者)。

    【讨论】:

    • 这是我怀疑的。使用 $.extend 方法编写一个有什么好处吗?
    • 据我所知,不,$.extend 对于将两个对象合并在一起很有用,但是当你只有 1 个“键”时,你要放入 { object },你是一个) 保证如果你稍后放它会覆盖它,并且 b) 不做任何其他有用的事情(比如合并来自另一个对象的键。不过,再一次,取决于你的喜好(但我会做第二种方式)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    • 2015-12-09
    • 1970-01-01
    • 2012-01-11
    • 2014-03-01
    • 1970-01-01
    相关资源
    最近更新 更多