JavaScript constructor属性

来源:互联网 发布:曼彻斯特 知乎 编辑:程序博客网 时间:2024/06/02 06:49
定义和用法

constructor 属性返回对创建此对象的(如数组,Date等类型)函数的引用。

 

<script type="text/javascript">var test=new Boolean();if (test.constructor==Array){document.write("This is an Array");}if (test.constructor==Boolean){document.write("This is a Boolean");}if (test.constructor==Date){document.write("This is a Date");}if (test.constructor==String){document.write("This is a String");}if (test.constructor==Number){document.write("This is a Number");}</script>


执行结果:

This is a Boolean

<html><body><script type="text/javascript">function employee(name,job,born){     this.name=name;    this.job=job;    this.born=born;}var bill=new employee("Bill Gates","Engineer",1985);document.write(bill.constructor);document.writeln("<br/>");var varr = new Array();document.write(varr.constructor);</script></body></html>


执行结果:

function employee(name,job,born) { this.name=name; this.job=job; this.born=born; }function Array() { [native code] } 


 

参考点击

0 0