一般情况下我们都用typeof来判断数据类型,但是javascript 中任何一个东西都是对象,如:
var a = new Array();
alert(typeof a); // object
var b = new Date();
alert(typeof b); // object

于是我们就只知道a b都是object,但是不知道这个对象究竟是什么名字,这时候constructor就可以帮助我了:

var a = new Array();
alert(a.constructor); // function Array(){..}

var b = new Date();
alert(b.
constructor); // function Date(){..}

比object具体多了,但是表达方式不太友好,用正则替换一下就行了:
var a = new Array();
alert(a.constructor.toString().match(/^function ([^(]*)/)[1]); // Array

var b = new Date();
alert(b.
constructor.toString().match(/^function ([^(]*)/)[1]); //  Date


Java » JavaScript | 评论(0) | 引用(0) | 阅读(111)
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]