深克隆

//定义函数 获取对象的构造函数(类)名
    function getObjectClass(obj) {
      return Object.prototype.toString.call(obj).slice(8, -1)
    }
    function deepClone(obj) {
      if (getObjectClass(obj) === "Object") {
        var res = {}  
      } else if (getObjectClass(obj) === "Array") {
        var res = [];
      } else {
        return obj
      }

      for (var i in obj) {
        res[i] = deepClone(obj[i]);
      }
      return res;
    }

猜你喜欢

转载自www.cnblogs.com/wangsai-666/p/12038243.html