zoukankan      html  css  js  c++  java
  • jQuery.extend

    扩展对象的属性,代码如下: 

    jQuery.extend = jQuery.fn.extend = function(){
        var options, name, src, copy, copyIsArray, clone,
            target = arguments[0] || {},
            //处理arguments数组的下标
            i = 1,
            length = arguments.length,
            deep = false;
    
        //如果第一个参数是布尔变量的话
        if( typeof target === "boolean"){
            deep = target;
            //总是确保target是对象
            target = arguments[ i ] || {}; 
            
            i++;
        }
    
        //非对象,非函数的情形
        if( typeof target !== "object" && !jQuery.isFunction( target ) ){
            target = {};
        }
    
        //首参数布尔是,i = 2, 非布尔时, i = 1
        //如果这时 i 与参数的长度相等。
        if( i === length ){
            target = this;
            i--;
        }
    
        for( ; i < length; i++ ){
            // 仅处理非空,定义的值
            if( (options = arguments[i] ) != null ){
    
                // Extend the base object 
                for( name in options ){
                    src = target[ name ];
                    copy = options[ name ];
    
                    //阻止无限循环
                    if( target === copy ){
                        continue;
                    }
    
                    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 : {};
                        }
    
                        target[ name ] = jQuery.extend( deep, clone, copy );
                    //不要把未定义的值进行赋值
                    } else if( copy !== undefiend ){
                        target[ name ] = copy;
                    }
                }
            }
        }
    
        return target;
    };
  • 相关阅读:
    LeetCode子集问题
    面试题-求最大字典区间
    链表快速排序
    树的非递归遍历
    快速排序非递归实现
    leetcode217 python3 72ms 存在重复元素
    leetcode121 C++ 12ms 买股票的最佳时机 只能买卖一次
    leetcode1 python3 76ms twoSum 360面试题
    leetcode485 python3 88ms 最大连续1的个数
    leetcode119 C++ 0ms 杨辉三角2
  • 原文地址:https://www.cnblogs.com/branches/p/4890606.html
Copyright © 2011-2022 走看看