数据类型
1、数据类型
基本数据类型:number string boolean null undefined
引用数据类型:Object
2、弱类型特性
- 隐式转换利用+ -
- ===
NAN != NAN null === null undifined === undifined 对象比较:比较的是引用
- ==
null == undifined
number == string (string 转化number) 1 == "1.0" //true
boolean == ? (boolean转化为number )
object == number || string (尝试对象转化为基本类型)
//既: object权限<boolean权限<string权限<number权限
故 遇见此情况 object先转换类型
null和undefined区别- null是空指针对象(在栈里没有存储地址)
- undedined是定义了但没有赋值
- ===
3、包装对象
- number string boolean 都有对象的包装类型
let str="字符串" ; let strObj=new String('字符串') str == strObj
对string进行方法操作时、js默认将其转化为对象形式(new String()方法)、操作完成立即删除这个对象
let a="string"
alert(a.length) //6
a.t=3
alert(a.t) //undefined
4、类型检测- typeof
适用于:函数类型 和基本类型
typeof null //"object (历史原因)"
typeof NAN //"number"
typeof undefined//''undefined"
- instanceof 左边是对象 右边数函数对象或者函数构造器
obj instanceof Object
原理:obj的原型链上是否有Object的prototype属性
在不同的window和iframe间检测失效
- Object.prototype.toString
Object.prototype.toString.apply([]) === "[object Array]"
Object.prototype.toString.apply(function(){}) === "[object Function]"
Object.prototype.toString.apply(null) === "[object null]"
Object.prototype.toString.apply(undefined) === "[object undefined]"
//在ie6/7/8下 Object.prototype.toString.apply(null) 返回照样是 "[object object]"”
- constructor
- duck type
- typeof