js数据类型
判断类型有以下4种判读方法
第一种方式: typeof
typeof null ---> "object"
typeof undefined ---> "undefined"
typeof true | false ---> 'boolean'
typeof 42 ---> 'number'
typeof "42" ---> 'string'
typeof { name : '1'} | [] ---> 'object'
typeof Symbol ---> 'symbol'
typeof ()=>{} ---> 'function'
typeif void 0 ---> 'undefined'
第二种方式 instanceof 但是这种方式只适合判断object类型
比如 : var arr = [] ; arr instanceof Array ---> true
arr instanceof Object --> true
null instanceof Object ---> false //虽然typeof为object,单null不是Ojbect的实例
[function] instanceof Object | Function --> true
第三种方式 Object.prototype.toString.call() 这种方式可以将全部的数据类型检测出来 也是 推荐的方式
因为toString是Object的原型方法, 而 Array Function 等都是Object的实例。都重写了toString 方法。返回的是类型的字符串
Object.prototype.toString.call(null) ---> [object Null]
Object.prototupe.toString.call(undefined) ---> [object Undefined]
Object.prototype.toString.call(123) ---> [object Number]
Object.prototype.toString.call(true) ---> [object Boolean]
Object.prototype.toString.call('123') ---> [object String]
Object.prototype.toString.call({}) ---> [object Object]
Object.prototype.toString.call([]) ---> [object Array]
Object.prototype.toString.call(Math) ---> [object Math]
Object.prototype.toString.call(function(){}) ---> [object Function]
Objdec.prototype.toString.call(new Date) ---> [object Date]
Object.prototype.toString.call(Symbol()) ---> [object Symbol]
{
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;
this.changeName=changeName;
function changeName(name)
{
this.lastname=name;
var x= new Number;
var y= new Boolean;
var cars= new Array;
var person= new Object;