首页 > javascript中用typeof测数据的类型,为什么没有null类型?并且new的全是object类型?

javascript中用typeof测数据的类型,为什么没有null类型?并且new的全是object类型?

1.数据类型不是就六种吗(number,String。。。。)
2.typeof判断数据类型
如果var x =null;的时候为什么判断出来时object类型?
3.只要是new出来的都是object类型?
如果var x = new Number();用typeof判断也是object类型呀?
4.var x = new Function();用typeof是function类型,可是数据类型中没有function类型呀?


《javascript高级程序设计》里面有说明(第23页),typeof null是object,null表示一个空对象指针,虽然ECMA-262规定对他们的相等性测试 alert(null == undifined); 要返回 true,但是他们的用途不同。


null被认为是空对象,使用typeof会返回Object

使用new Function构造函数的方式创建的实例都是对象。

var x = new Number();使用typeof会返回Object

使用typeof是能检测类型,但此类型不是javascript分出来的那六种类型。typeof多用于检测基本类型。检测对象类型使用的多是instanceof

typeof undefined;//undefined
typeof true;//boolean
typeof 'haha'//string
typeof 1;//number

var a = new Function();
a instanceof Function;//true
a instanceof Object;//true

Person() {}//声明自定义函数
var b = new Person();
b instanceof Person;//true
b instanceof Object;//true    

null是本该有但没有。
undefined是没有而且不知道有没有。
所以null是anything,是object。
undefined就是undefined。


1.js 除了undefined 都是对象

2.js分为原始类型(number,string)和引用类型(function, object)
【热门文章】
【热门文章】