function person(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
}
var myFather = new person("John", "Doe", 50, "blue");
console.log(myFather instanceof person); //true
console.log(myFather instanceof Object); //true
console.log(myFather instanceof Function); //false
你好,在这种情况下,我们从函数构造函数创建了一个对象:'person'。
JavaScript中的每个函数都是Function构造函数的一个实例。 为什么myFather不是Function的实例?
答案 0 :(得分:3)
myFather
是person
的对象实例,这就是为myFather instanceof Object
返回true但myFather instanceof Function
为false的原因,因为它不是函数而是对象,你无法再次调用myFather来实例化另一个对象。实际上person
是Function的一个实例。当您调用new person
时,将返回一个普通对象,并将其存储在myFather中。
function person(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
}
var myFather = new person("John", "Doe", 50, "blue");
console.log(myFather instanceof person); //true
console.log(myFather instanceof Object); //true
console.log(myFather instanceof Function); //false
console.log(person instanceof Function); //true