[英]Why does typeof Array returns “function” and typeof [array variable] returns an “Object”?
I had a chance to met this question but unable to find an answer.我有机会遇到这个问题,但无法找到答案。
var arr = [];
Why does typeof Array
returns "function"
and typeof arr
returns an "Object"
?为什么
typeof Array
返回"function"
而typeof arr
返回"Object"
?
Can anyone explain please.谁能解释一下。
When you write typeof Array
, it means that you are getting type of constructor function.当您编写
typeof Array
时,这意味着您正在获取构造函数 function 的类型。 As class
is under the hood is constructor function.由于
class
在引擎盖下是构造函数 function。 Let me show an example:让我举个例子:
class Person {
constructor(firstName, lastName, address) {
this.firstName= firstName;
this.lastName = lastName;
this.address= address;
}
getFullName () {
return this.firstName + " " + this.lastName ;
}
}
and to create an instance of the class:并创建 class 的实例:
let car = new Person ("Jon", "Freeman", "New York");
In the above code, we've created a variable car
which references to function construtor defined in the class:在上面的代码中,我们创建了一个变量
car
,它引用了 class 中定义的 function 构造函数:
function Person (firstName, lastName, address) {
this.firstName = firstName,
this.lastName = lastName,
this.address = address,
this.getFullName = function () {
return this.firstName+ " " + this.lastName;
}
}
So this is a reason why typeof Array
returns function
.所以这就是
typeof Array
返回function
的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.