在Java中,int,float,double都不是对象.
在JS中,一切皆对象.每一个对象,都有自己的属性和方法.
- 简单类型(typeof 对象)
- typeof 1/str 返回number/string 两个是典型的简单类型没有proto属性
- typeof 类/函数 返回function 在js中类就是函数
- typeof new出的对象 返回object
var res1=typeof 1;//number
var res2=typeof 'armo';//string
//数组对象是属于引用类型的对象,[1,2,3]是简写方式. 真正写法是new Array(1,2,3)
var res3=typeof [1,2,4];//object
var res4=typeof function(){};//function
var res5=typeof String;//function
- 引用类型(对象 instanceof 类(即function对象))
- 简单类型不能用instanceof来判断
- 实质是判断 对象.proto==类.prototype?
- instance后只能跟typeof 是function的对象.
var res1= new String('1') instanceof String; //true
var res2= new Number(1) instanceof Number;//true
var res3= 'str' instanceof String;//false
var res4= 1 instanceof Number;//false
- 结合typeof 和instanceof来判断对象是什么
var dowork=function(pram){
if(pram instanceof String|| typeof pram=='string'){return '字符串'}
else if(pram instanceof Date){return '时间'} //由于 typeof (时间对象/数组对象) ==object 不用写
else if(pram instanceof Array){return '数组'}
else if(pram instanceof Number|| typeof pram=='number'){return '数字'}
else if(pram instanceof Function|| typeof pram=='function'){return '函数'}
}