Home > Web Front-end > JS Tutorial > Implement deep copy and shallow copy in jquery

Implement deep copy and shallow copy in jquery

一个新手
Release: 2017-09-22 09:43:28
Original
1807 people have browsed it


var toString = [].toString,
    hasOwn = Object.prototype.hasOwnProperty;var types = {    
    '[object Boolean]':'bool',    
    '[object Number]' : 'number',  
        '[object String]' : 'string',  
    '[object Object]':'object',    
    '[object Array]':'array',    
    '[object Function]':'function',    
    '[object Date]' : 'date', 
            '[object RegExp]' : 'regExp',  
};var type = function(obj){
    return obj === null ? String(obj) : types[toString.call(obj)] || 'object'; 
};var isWindow = function(obj){
    return obj && typeof obj === 'object' && 'setInterval' in obj;
};var isArray = Array.isArray || function(obj){
    return type(obj) === 'array';
};var isPlainObject = function(obj){
    if(!obj || type(obj) !== 'object' || isWindow(obj) || obj.nodeType){        
    return false;
    }    
    try{        
    if(obj.constructor && !hasOwn.call(obj, 'constructor') 
            && !hasOwn.call(obj.constructor.prototype, 'isPrototypeOf')){            
            return false;
        }
    }catch(e){        
    return false
    }    
    var key;    for(key in obj){}    return key === undefined || hasOwn.call(obj, key)
}function extend(deep, target, ref){
    var name, value, src, copy, clone, copyIsArray;    for(name in ref){
        src = target[name];
        copy = ref[name];        
        if(target === copy){            
        continue;
        }        
        if(deep && copy && (isPlainObject(copy) || (copyIsArray = isArray(copy)))){            
        if(copyIsArray){
                clone = src && isArray(src) ? src : [];
            }else{
                clone = src && isPlainObject(src) ? src : {}; 
            }
            target[name] = extend(deep,clone,copy);
        }else{
            target[name] = copy;
        }
    }    return target;
}function extend2(target, ref){
    var name, value;    
    for(name in ref){
        value = ref[name];        
        if(value !== undefined){
            target[name] = value;
        }
    }    return target;
}
Copy after login

The above is the detailed content of Implement deep copy and shallow copy in jquery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template