你不知道的JavaScript(中卷)
1.类型
1.1 内置类型
null,string,undefined, Boolean, number,object, symbol(除object外2,其他为基本类型)
1.2 typeof
typeof undefined === "undefined" // true
typeof true === "boolean" // true
typeof 42 === "number" // true
typeof "42" === "string" // true
typeof { name: "ray" } === "object" // true
typeof null === "object" // true 数组也是对象
1.3 undefined 和 undeclared
var a
console.log(a) // undefined未定义
console.log(b) // undeclared未声明
2.值
2.1 数组
var a = []
a[0] = 1
a['floor'] = 2
a.length // 1
Array.from()方法就是将一个类数组对象或者可遍历对象转换成一个真正的数组。
所谓类数组对象,最基本的要求就是具有length属性的对象。
2.2 字符串
字符串也可以使用concat()
2.3 特殊的数字
var a = 2/'floor' //NaN
typeof a === 'number'// true
var a = 0 / -1 //-0
var b = 0 * -1 //-0
0 === -0
0>-0 //false
2.4 instanceof
instanceof 运算符用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链上。
3.类型转换
3.1 值类型转换
var a = 42
var b = a + '' //隐式强制类型转换
var c = String(a) // //显式强制类型转换
3.2 宽松相等和严格相等
==允许在相等比较中进行强制类型转换,而===不允许

浙公网安备 33010602011771号