如果我们从函数构造函数创建一个名为 'a' 的对象,那么为什么 'a' 不是 Function 的实例?
If we create an object called 'a' from a function constructor, then why is 'a' not an instance of Function?
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 的实例?
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
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 的实例?
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