.toString()——把对象转成字符串、进制转换、判断对象类型

谁拥有:系统对象、自己创建的对象

位置:系统对象在 对象.prototype下 , 自己写的对象都是通过原型链找Object.prototype下面的

作用:

1、把对象转成字符串

var arr = [1,2,3];
alert( typeof arr.toString() );   //string
alert( arr.toString() );  //1,2,3

2、进制转换

var num = 255;
alert( num.toString(16) );  //'ff',把num转换成16进制的字符串

3、类型判断

(1)判断是不是数组

写法:Object.prototype.toString.call(对象) == '[object Array]' 

var arr = [];
alert( Object.prototype.toString.call(arr) == '[object Array]' );   //true

(2)判断是不是json

写法:Object.prototype.toString.call(对象) == '[object object]' 

var arr = {};
alert( Object.prototype.toString.call(arr) == '[object object]' );   //true

(3)判断是不是时间

写法:Object.prototype.toString.call(对象) == '[object Date]' 

var arr = new Date();
alert( Object.prototype.toString.call(arr) == '[object Date]' );   //true

(4)判断是不是正则

写法:Object.prototype.toString.call(对象) == '[object RegExp]' 

var arr = new RegExp();
alert( Object.prototype.toString.call(arr) == '[object RegExp]' );   //true

(5)判断是不是空

写法:Object.prototype.toString.call(对象) == '[object Null]' 

var arr = new null;
alert( Object.prototype.toString.call(arr) == '[object Null]' );   //true

 

posted @ 2017-12-07 13:43  念念念不忘  阅读(773)  评论(0)    收藏  举报