JavaScript array constructor 属性


定义和用法

constructor 属性返回对创建此对象的数组函数的引用。

语法
  1. object.constructor

实例

在本例中,我们将展示如何使用 constructor 属性:

  1. <html>
  2. <body>
  3. <script type="text/javascript">
  4. var test=new Array();
  5. if (test.constructor==Array)
  6. {
  7. document.write("This is an Array");
  8. }
  9. if (test.constructor==Boolean)
  10. {
  11. document.write("This is a Boolean");
  12. }
  13. if (test.constructor==Date)
  14. {
  15. document.write("This is a Date");
  16. }
  17. if (test.constructor==String)
  18. {
  19. document.write("This is a String");
  20. }
  21. </script>
  22. </body>
  23. </html>

输出:

  1. This is an Array
例子 2

在本例中,我们将展示如何使用 constructor 属性:

  1. <html>
  2. <body>
  3. <script type="text/javascript">
  4. function employee(name,job,born)
  5. {
  6. this.name=name;
  7. this.job=job;
  8. this.born=born;
  9. }
  10. var person=new employee("埃隆 马斯克","企业家",1981);
  11. document.write(person.constructor);
  12. </script>
  13. </body>
  14. </html>

输出:

  1. function employee(name, job, born)
  2. {this.name = name; this.job = job; this.born = born;}

分类导航