gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 19:03:27 24 4
gpt4 key购买 nike

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

最佳答案

myFatherperson 的对象实例,这就是为什么它为 myFather instanceof Object 返回 true 而为 myFather instanceof 返回 false 的原因Function 因为它不是一个函数而是一个对象,你不能再次调用 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

关于javascript - 如果我们从函数构造函数创建一个名为 'a' 的对象,那么为什么 'a' 不是 Function 的实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44986476/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com