-
变量分为值类型和引用类型
-
值类型和引用类型的区别
const obj1 = {х:100,у:200} const obj2 = obj1 let x1 = obj1.x obj2.x = 101 x1 = 102 console.log(obj1) // { x:101 }
-
对象、数组都是引用类型,null是特殊的引用类型,指针指向空地址
-
函数是一个特殊引用类型,并不用于存储数据,所以没有“拷贝、复制函数”的说法。
-
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' ES6 //能判断函数 typeof console.log // 'function' typeof function () {} // 'function' //能识别引用类型(不能再继续识别) typeof null // 'object ' typeof ['a','b'] // 'object' typeof { x:100 } // 'object'
-
类型转换
-
字符串拼接
const a = 100 + 10 // 110 const b = 100 +'10' // ' 10010' const C = true + '10'// true10 !
-
-
== 和 ===
- ==
- ===
- 代码示例
// == 运算符会做一些隐式的类型转换,尽量让两边相等 100 == '100' // true 0 == '' // true 0 == false // true false == '' // true null == undefined // true // 三个等号会强制使两者一致,除了 == null 之外,其他都一律用===,例如: const obj = { x:100 } if (obj.a == null) { } // 相当于: // if (obj.a === null || obj.a === undefined) { } //除了 == null之外,其他都一律用 === , 例如: const obj = { x:100 } if (obj.a == null) { } // 相当于: // if (obj.a === null | | obj.a == undefined){}
-
if语句和逻辑运算
//以下是 falsely 变量。除此之外都是 truly 变量 !!0 === false !!NaN ===false !!'' === false !!null === false !!undefined === false !!false === false
console.log(10 && 0) // 0 console.log( ''||'abc' ) // 'abc' console.log( !window. abc ) // true
-
小结
- 值类型 VS 引用类型、堆栈模型、深拷贝
- typeof 运算符
- 类型转换、truly 和 falsely 变量
【前端面试指南】JS-1-类型
最新推荐文章于 2024-05-24 15:13:38 发布