深拷贝和类型检测

      //  深拷贝
        var user1={name:"小明",age:18, deMent:{bumen:"市场部",no:"001"}}
         
         var  user2=JSON.stringify(user1);
         var  user3=JSON.parse(user2);

         user3.age=40;
         user3.deMent.no="999";


         console.log(user1); //{name: "小明", age: 18, deMent:{bumen: "市场部", no: "001"}}
         console.log(user3) //{name: "小明", age: 40,deMent: {bumen: "市场部", no: "999"}}

         // 深拷贝的步骤 JSON.stringify(被拷贝的对象); 把对象转为一个字符串
         // JSON.parse(user2);会根据字符串生成一个新的对象

         // 类型检测
         /*
         基本数据类型检测使用typeof来检测    console.log(变量 typeof 基本类型)
         引用数据类型使用instanceof来检测    console.log(函数 instanceof Function)
         */

         // 检测数组
         var arr=[1,2,3,4,,5];
         console.log(Array.isArray(arr)); //true

猜你喜欢

转载自www.cnblogs.com/IwishIcould/p/11566237.html