1

它似乎不是抽象对象或对象文字。

我使用以下值从下划线测试了 isObject() :

var falsy = [
        false,
        '', "",
        0, -0,
        undefined, null,
        NaN
    ],
    // 6 truthy
    truthy = [
        true,
        'hello',
        -1, 1,
        Infinity,
        /test/
    ],
    // 3 lengthy
    lengthy = [
        function () {
            return undefined;
        },
        "string",
        [0]
    ],
    // 8 globals
    globals = [
        new String(),
        new Number(),
        new Boolean(),
        new Array(),
        new Object(),
        new Function(),
        new Date(),
        new RegExp()
    ],
    all = falsy.concat(truthy, lengthy, globals),

我都用过。

这是我从控制台返回的。

在此处输入图像描述

我发现最奇怪的是一个字符串没有被检测为一个对象,认为它显然不是基本/原始类型并且可以保存属性。

这似乎是isObject().

如果事实是这件事在做什么呢?

4

1 回答 1

0

It does in fact just return true for Objects and false for primitives.

Javascript has 5 primitives:

undefined, null, boolean, string and number

However, the language also has Object versions of some of these primitives eg String, Number. So "foo" is a primitive but new String("bar") is an object.

The difference between primitives and objects is often hidden by the fact that javascript uses coercion to automatically convert primitives into objects and to convert objects into primitives when necessary.

于 2013-11-06T18:13:40.920 回答