zoukankan      html  css  js  c++  java
  • js 递归实现深度克隆与浅克隆

    直接看代码吧,浅显易懂并配有英文注解,

    
    	/**
    	 * Iterate over an Array or an Object invoking a function for each item.
    	 *
    	 * If `obj` is an Array callback will be called passing
    	 * the value, index, and complete array for each item.
    	 *
    	 * If 'obj' is an Object callback will be called passing
    	 * the value, key, and complete object for each property.
    	 *
    	 * @param {Object|Array} obj The object to iterate
    	 * @param {Function} fn The callback to invoke for each item
    	 */
    	function forEach(obj, fn) {
    	  // Don't bother if no value provided
    	  if (obj === null || typeof obj === 'undefined') {
    	    return;
    	  }
    	
    	  // Force an array if not already something iterable
    	  if (typeof obj !== 'object') {
    	    /*eslint no-param-reassign:0*/
    	    obj = [obj];
    	  }
    	
    	  if (isArray(obj)) {
    	    // Iterate over array values
    	    for (var i = 0, l = obj.length; i < l; i++) {
    	      fn.call(null, obj[i], i, obj);
    	    }
    	  } else {
    	    // Iterate over object keys
    	    for (var key in obj) {
    	      if (Object.prototype.hasOwnProperty.call(obj, key)) {
    	        fn.call(null, obj[key], key, obj);
    	      }
    	    }
    	  }
    	}
    	/**
    	 * Accepts varargs expecting each argument to be an object, then
    	 * immutably merges the properties of each object and returns result.
    	 *
    	 * When multiple objects contain the same key the later object in
    	 * the arguments list will take precedence.
    	 *
    	 * Example:
    	 *
    	 * ```js
    	 * var result = merge({foo: 123}, {foo: 456});
    	 * console.log(result.foo); // outputs 456
    	 * ```
    	 *
    	 * @param {Object} obj1 Object to merge
    	 * @returns {Object} Result of all merge properties
    	 */
    	function merge(/* obj1, obj2, obj3, ... */) {
    	  var result = {};
    	  function assignValue(val, key) {
    	    if (typeof result[key] === 'object' && typeof val === 'object') {
    	      result[key] = merge(result[key], val);
    	    } else {
    	      result[key] = val;
    	    }
    	  }
    	
    	  for (var i = 0, l = arguments.length; i < l; i++) {
    	    forEach(arguments[i], assignValue);
    	  }
    	  return result;
    	}
    	
    	/**
    	 * Function equal to merge with the difference being that no reference
    	 * to original objects is kept.
    	 *
    	 * @see merge
    	 * @param {Object} obj1 Object to merge
    	 * @returns {Object} Result of all merge properties
    	 */
    	function deepMerge(/* obj1, obj2, obj3, ... */) {
    	  var result = {};
    	  function assignValue(val, key) {
    	    if (typeof result[key] === 'object' && typeof val === 'object') {
    	      result[key] = deepMerge(result[key], val);
    	    } else if (typeof val === 'object') {
    	      result[key] = deepMerge({}, val);
    	    } else {
    	      result[key] = val;
    	    }
    	  }
    	
    	  for (var i = 0, l = arguments.length; i < l; i++) {
    	    forEach(arguments[i], assignValue);
    	  }
    	  return result;
    	}
    

    代码来源注释:作者在看axios源码时看到的片段。

  • 相关阅读:
    搭建CentOS在线yum源镜像服务器
    zabbix-使用orabbix来监控oracle11g
    zabbix监控系统-1:系统搭建
    ELK-学习-1:elasticsearch6.3安装和配置
    zabbix-使用percona mysql插件来监控mysql
    zabbix学习基础篇-4:用户群组和用户
    相关性分析
    马尔可夫预测
    该工程中的宏被禁止,请参阅联机帮助
    matlab工具包
  • 原文地址:https://www.cnblogs.com/xiaolantian/p/13282988.html
Copyright © 2011-2022 走看看