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;}