JavaScript对象属性 constructor

对象属性

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

  constructor(构造函数) 在对象创建或实例化时候被调用的方法.通常使用该方法来初始化数据成员和所需资源。构造函数不能被继承;

构造函数是一种特殊的方法,主要用来在创建对象时初始化对象,即为对象成员变量赋初始值.总与new运算符一起使用在创建对象语句中,特别的一个类可以有多个构造函数,可根据其参数个数的不同或参数类型的不同来区分它们,即构造函数的重载;

<scirpt>

var test = new Array();

if (test.constructor == Array){

    document.write('This is an Array');      //test为数组返回当前

}

if (test.constructor == Boolean){

    document.write('This is an Boolean');     //test为布尔值则返回当前

}

if (test.constructor == Date){

    document.write('This is an Date');        //test为Data时间则返回当前
  
}

if (test.constructor == String){

    document.write('This is an String');     //test为字符串则返回当前

}

</script>

 

 在JavaScript中每个函数都有名为‘prototype'的属性,用于引用原型对象.此原型对象又有名为'constructor'的属性它反过来引用函数的本身这是一种循环使用

 function Animal(){} 

 function Person(){} 


 Person.prototype = new Animal(); 
 var person = new Person(); 


 alert(person.constructor);                               //Animal