Js数据类型判断

JavaScript 何求龙
文章标签: Js数据类型判断

数据类型判断:

1.typeof

typeof有两种写法:分别是typeof xxx和typeof(xxx);

实例:	
    typeof	2      		输出	number
    typeof	null   		输出	object
    typeof	undefined	输出	undefined
    typeof	'222'		输出    string
    typeof	true		输出	boolean
    typeof	(function(){})	输出	function

--这里面包含了js里面的五种数据类型  number   string    boolean   undefined     object和函数类型 function;

2.Object.prototype.toString.call

这是对象的一个原生原型扩展函数,用来更精确的区分数据类型;

实例:	var   gettype=Object.prototype.toString;
	        gettype.call('aaaa')		输出	[object String]
        	gettype.call(2222)		输出	[object Number]
        	gettype.call(true)		输出	[object Boolean]
        	gettype.call(undefined)		输出	[object Undefined]
        	gettype.call(null)		输出   	[object Null]
         	gettype.call({})		输出   	[object Object]
         	gettype.call([])		输出	[object Array]
         	gettype.call(function(){})     	输出	[object Function]

3.constructor

实例:	''.constructor === String	相等输出true否则输出false
	[].constructor === String	相等输出true否则输出false

还能输出{{restrictNumber}}个字符  
  • {{reply.author}}

    {{CommonUtil.formateDate(reply.ac_CommentDate).shortTime}}
  • 回复了{{Comments.author}} :