typeof运算符和深拷贝
typeof运算符
- 识别所有值类型
- 识别函数
- 判断是否是引用类型(不可再细分)
//判断所有值类型 let a; typeof a //'undefined' const str='abc'; typeof str //'string' const n=100; typeof n //'number' const b=true; typeof b //'boolean' const s=Symbol('s') typeof s //'symbol' //能判断函数 typeof console.log //'function' typeof function(){} //'function' //能换成别引用类型(不能再继续识别) typeof null //'object' typeof ['a','b'] //'object' typeof {x:100} //'object'
深拷贝
/**
*深拷贝
*/
function deepClone(obj={}){if(typeof obj!=='object' || obj==null){return obj;}//初始化返回结果let resultif(obj instanceof Array){result=[];}else{result={};}for(var key in obj){if(obj.hasOwnProperty(key)){result[key]=deepClone(obj[key])}}//返回结果return result;}