Javascript 为什么typeof数组返回“1”;功能“;和typeof[array variable]返回一个";对象";?

Javascript 为什么typeof数组返回“1”;功能“;和typeof[array variable]返回一个";对象";?,javascript,arrays,Javascript,Arrays,我有机会遇到这个问题,但找不到答案 var arr = []; 为什么typeof Array返回“函数”而typeof arr返回“对象” 有人能解释一下吗。当你写数组的类型时,这意味着你得到了构造函数的类型。由于类在引擎盖下,因此是构造函数。让我举一个例子: class Person { constructor(firstName, lastName, address) { this.firstName= firstName; this.lastNa

我有机会遇到这个问题,但找不到答案

var arr = [];
为什么
typeof Array
返回
“函数”
typeof arr
返回
“对象”


有人能解释一下吗。

当你写数组的类型时,这意味着你得到了构造函数的类型。由于
在引擎盖下,因此是构造函数。让我举一个例子:

class Person {
    constructor(firstName, lastName, address) {
        this.firstName= firstName;
        this.lastName = lastName;
        this.address= address;
    }

    getFullName () {
        return this.firstName + " " + this.lastName ;
    }
}
要创建该类的实例,请执行以下操作:

let car = new Person ("Jon", "Freeman", "New York");
function Person (firstName, lastName, address) {
        this.firstName = firstName,
        this.lastName = lastName,
        this.address = address,
        this.getFullName = function () {
            return this.firstName+ " " + this.lastName;
        }
}
在上面的代码中,我们创建了一个变量
car
,它引用了类中定义的函数construtor:

let car = new Person ("Jon", "Freeman", "New York");
function Person (firstName, lastName, address) {
        this.firstName = firstName,
        this.lastName = lastName,
        this.address = address,
        this.getFullName = function () {
            return this.firstName+ " " + this.lastName;
        }
}

这就是为什么
typeof Array
返回
function

的原因,因为
Array
是数组实例的构造函数,数组实例最终是对象(),您可以调用
Array
arr=new Array()。因为它是可调用的,所以它是一个函数。@PatrickEvans请您详细说明一下。。另外,不确定为什么typeof arr是一个对象。我们将“数组”作为数据类型。。所以它假设返回“array”。每个数组都是
数组
类的一个实例,因此它是一个对象。要检查对象是否是数组,您应该使用类似于
array.isArray
的东西,而不是
typeof
@Ayrton JavaScript可能会让人困惑。最好不要引入不适用于它的词语。而且,你没有在答案中加引号;)