二. 题
1. typeof判断类型(识别所有值类型,识别函数,判断是否是引用类型),instanceof,constructor,toString,
-
typeof undefined //undefined
typeof null //object
typeof 3.4 //number
typeof 'abc' //string
typeof true//boolean
typeof f //function
typeof arr //object
typeof {} //object
-
instanceof只能判断出来引用类型,判断不出来简单类型
[] instanceof Array//true let obj = {} obj instanceof Objecet//true function f (){} f instanceof Function//true -
constructor,不能识别undefined,null
''.constructor//ƒ String() { [native code] } false.constructor//ƒ Boolean() { [native code] } let num = 1 num.constructor//ƒ Number() { [native code] } [].constructor//ƒ Array() { [native code] } f.constructor//ƒ Function() { [native code] } let obj = {name:1} obj.constructor//ƒ Object() { [native code] } -
toString,都能识别出来
Object.prototype.toString.call(null)//"[object Null]" Object.prototype.toString.call(undefined)//"[object Undefined]" Object.prototype.toString.call('')//"[object String]" Object.prototype.toString.call(false)//"[object Boolean]" Object.prototype.toString.call(1)//"[object Number]" Object.prototype.toString.call([])//"[object Array]" Object.prototype.toString.call({})//"[object Object]" Object.prototype.toString.call(f)//"[object Function]"
2. 0.1 + 0.2 === 0.3//false
2. 值类型与引用类型的区别
- 值类型赋值是值,引用类型赋值是地址
- 值类型是栈,引用类型是栈中存放地址,堆中存放数据,指向栈也就是地址
2. 字符串拼接
const a = 100 + '10'//'10010'
const b = true + '10'//'true10'
3. 强制类型转换和隐式类型转换
- 强制: parseInt,parseFloat ,toString 等
- 隐式:if,逻辑运算, ==,+字符串拼接
3. ==和===,只有==null时用==,其他的都用===,===数据类型也得一样,==会将数据类型自动转换
100 == '100'//true
null == undefined//true
0 == false//true
0 == ''//true
'' == false//true
if(obj.a == null)//相当于obj.a === null || obj.a === undefined,所以用==简便
100 === '100'//false
null === undefined//false
0 === false//false
0 === ''//false
'' === false//false
4. truly和falsely变量,通过!! index=== true就是truly变量,!! index === false就是falsely变量
-
以下都是falsely变量,除此以外都是truly变量
!!0 === false !!false === false !!'' === false !!undefined === false !!null === false !!NaN === falseif内容判断,&&,||,!都是用truly,falsely变量