美文网首页
面向对象4

面向对象4

作者: 追逐_e6cf | 来源:发表于2018-10-01 15:48 被阅读0次

    一、判断数组和对象

    [].constructor
    f Array(){[native code]}
    
    var obj = {}
    obj.constructor
    f Object(){[native code]}
    
    [] instanceof Array
    true
    
    obj instanceof Array
    false
    
    Object.prototype.toString.call([])
    "[object Array]"
    
    Object.prototype.toString.call({})
    "[object Object]"
    

    二、浅克隆

    var zhangsan = {
      name:"zhangsan",
      age: 100,
      sex:undefined,
      like:["smoke", "drink"],
      girlFriend:{
        girlFriend1:{
          name:"xiaozhang"
        },
        girlFriend2:{
          name:"xiaoli"
        }
      }
    }
    
    var obj = {};
    
    for(var key in zhangsan){
      if(zhangsan.hasOwnProperty(key)){
        obj[key] = zhangsan[key]
      }
    }
    
    obj.girlFriend.girlFriend3 = {
      name:"xiaowang"
    }
    

    三、深克隆

    //1.判断是不是typeof == object
    //2.判断数组还是对象 [] || {}
    //3.递归
    function deepClone(origin, target){
      var target = target || {};
      for(var key in origin){
        if(origin.hasOwnProperty(key)){
          if(typeof(origin[key]) == "object"){
            if(Object.prototype.toString.call(origin[key]) == "object Array"){
              target[key] = [];
            }else{
              target[key] = {};
            }
            deepClone(origin[key], target[key]);
          }else{
            target[key] = origin[key];
          }
        }
      }
      return target;
    }
    
    function deepClone(origin, target){
      var target = target || {};
      for(var key in origin){
        if(origin.hasOwnProperty(key)){
          if(origin[key] !== null && typeof(origin[key]) == "object"){
            target[key] = (Object.prototype.toString.call(origin[key]) == "[object Array]") ? [] : {};
            deepClone(origin[key], target[key]);
          }else{
            target[key] = origin[key];
          }
        }
      }
      return target;
    }
    
    var zhangsan = {
      name:"zhangsan",
      age: 100,
      sex:undefined,
      like:["smoke", "drink"],
      girlFriend:{
        girlFriend1:{
          name:"xiaozhang"
        },
        girlFriend2:{
          name:"xiaoli"
        }
      }
    }
    
    var obj = {};
    
    deepClone(zhangsan, obj);
    obj.like.push("女");
    zhangsan.girlFriend.girlFriend3 = {
      name : "xiaowang"
    }
    

    四、引用值

    var arr = [1, 2, 3, 4];
    var arr1 = arr;
    arr1.push(0);
    
    var obj = {
      name : 0
    }
    var obj1 = obj;
    obj1.age = 100;
    

    五、类数组

    var obj = {
      "2" : "b",
      "3" : "c",
      length : 2,
      push : Array.prototype.push
    }
    obj.push("d");
    obj.push("e");
    
    obj = {
      "0" : 1,
      "1" : 2,
      "2" : 3,
      name : "abc",
      age : "18",
      length : 100,
      push : Array.prototype.push,
      splice : Array.prototype.splice
    }
    

    相关文章

      网友评论

          本文标题:面向对象4

          本文链接:https://www.haomeiwen.com/subject/gcjnoftx.html