为什么typeof String返回函数

Rob*_*cha 5 javascript

为什么:

console.log( typeof String );function当它是一个物体时 返回?

uno*_*obf 6

String是字符串对象的构造函数.所有构造函数都是函数,因此是您看到的返回值.

你可以通过创建这样的代码来自己看到:

var MyObject = function (value) {
    this.value = value;
};

MyObject.prototype.getValue = function () {
    return this.value;
}

console.log(typeof(MyObject)); // function
console.log(typeof(new MyObject(1))); // object
Run Code Online (Sandbox Code Playgroud)