对象的constructor属性

对象的constructor属性, 最初是用来标识对象类型的。this

好比 ,咱们定义一个 Person类,而后实例化两个对象。
function Person(name, age, job){
this.name = name;
this.age = age;
this.job = job;
this.sayName = function(){
alert(this.name);
};
}
var person1 = new Person(“Jam”, 25, “Software Engineer”);
var person2 = new Person(“Tom”, 27, “Doctor”);
 
这两个对象 都有 一个constructor 属性,该属性指向 Person
lert(person1.constructor == Person); //true
alert(person2.constructor == Person); //true
 
不过,提到对象类型检测,咱们通常不这样作, 
由于 instanceof 操做符 更可靠一些
每每是这样用 alert(person1 instanceof Person); //true
相关文章
相关标签/搜索