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 中的每个函数都是函数构造函数的一个实例。 为什么 myFather 不是 Function 的实例?
最佳答案
myFather
是 person
的对象实例,这就是为什么它对 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/