当前位置: 移动技术网 > IT编程>开发语言>JavaScript > jQuery extend 的简单实例

jQuery extend 的简单实例

2018年12月07日  | 移动技术网IT编程  | 我要评论

代码如下:


jquery.extend = jquery.fn.extend = function() {
    var options, name, src, copy, copyisarray, clone,
        target = arguments[0] || {},
        i = 1,
        length = arguments.length,
        deep = false;

 

    // 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 plain objects or arrays
                if ( deep && copy && ( jquery.isplainobject(copy) || (copyisarray = jquery.isarray(copy)) ) ) {
                    if ( copyisarray ) {
                        copyisarray = false;
                        clone = src && jquery.isarray(src) ? src : [];

                    } else {
                        clone = src && jquery.isplainobject(src) ? src : {};
                    }

                    // 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;
};


如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网