题目
typeof 能判断哪些类型
何时使用===
何时使用==
值类型和引用类型的区别
手写深拷贝
值类型
1 2 3 4 5 let a = 100 let b = a a = 200 console .log (b)
引用类型
1 2 3 4 5 let a = {age : 20 }let b = a b.age = 21 console .log (a.age )
常见值类型 1 2 3 4 5 let a let s = 'abc' let n = 100 let b = true let s = Symbol ('s' )
常见引用类型 1 2 3 4 5 const obj = {x :100 }const arr = {'a' ,'b' ,'c' }const n = null function fn ( ){}
typeof 运算符
识别所有有值类型
识别函数
判断是否是引用类型(不可细分)
1 2 3 4 5 6 7 8 9 10 11 let atypeof a const str = 'abc' typeof str const n = 100 typeof n const b = true typeof b const s = Symbol ('s' )typeof s
1 2 3 4 5 6 7 typeof console .log typeof function ( ){} typeof null typeof ['a' ,'b' ] typeof {x :100 }
深拷贝代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 function deepClone (obj = {} ){ if (typeof obj !== 'object' || typeof obj == null ){ return obj } let result if (obj instanceof Array ){ result = [] }else { result = {} } for (let key in obj){ if (obj.hasOwnProperty (key)){ result[key] = deepClone (obj[key]) } } return result }
类型转换
1 2 3 const a = 100 + 10 const b = 100 + '10' const c = true + '10'
1 2 3 4 5 6 100 == '100' 0 == '' 0 == false false == '' null == undefined
1 2 3 4 5 const obj = {x :100 }if (obj.a == null ){}
if语句和逻辑运算
truly变量:!!a === true的变量
false变量:!!a === false的变量
1 2 3 4 5 6 7 !!0 === false !!'' === false !!NaN === false !!null === false !!undefined === false !!false === false
if语句判断的就是truely变量和falsely变量
小结
值类型 vs 引用类型,堆栈模型,深拷贝
typeof运算符
类型转换,truly 和falsely变量