如果我们从函数构造函数创建一个名为'a'的对象,那么为什么'a'不是Function的实例?

时间:2017-07-08 12:57:41

标签: javascript function function-constructor

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的实例?

1 个答案:

答案 0 :(得分:3)

myFatherperson的对象实例,这就是为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