2

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

4

1 回答 1

3

myFather是一个对象实例,person这就是为什么它返回 truemyFather instanceof Object而 falsemyFather instanceof Function因为它不是一个函数而是一个对象,你不能再次调用 myFather 来实例化另一个对象。实际上person是函数的一个实例。当你调用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

于 2017-07-08T13:01:14.470 回答