Does a matter whether a value is primitive or boxed

后端 未结 3 507
眼角桃花
眼角桃花 2021-01-01 20:49

One can use typeof to determine whether a value is primitive or boxed.

Consider:

typeof \"foo\"; // \"string\"
typeof new String(\"foo\         


        
3条回答
  •  一向
    一向 (楼主)
    2021-01-01 21:13

    I'd say there's virtually no point, you almost never care whether you're dealing with a string primitive or a String object.

    There are edge cases. For instance, a String object is an actual object, you can add properties to it. This lets you do things like this:

    function test(arg) {
        arg.foo = "bar";
    }
    

    If calling code passes in a string primitive:

    var s1 = "str";
    test(s1);
    

    ...arg gets promoted to a String object and gets a property added to it, but that String object isn't used by anything after test returns.

    In contrast, if calling code passes in a String object:

    var s2 = new String("str");
    test(s2);
    

    ...then the property is added to that object and the calling code can see it. Consider (live copy):

    var s1, s2;
    
    s1 = "str";
    
    display("[Before] typeof s1.foo = " + typeof s1.foo);
    test(s1);
    display("[After] typeof s1.foo = " + typeof s1.foo);
    
    s2 = new String("str");
    
    display("[Before] typeof s2.foo = " + typeof s2.foo);
    test(s2);
    display("[After] typeof s2.foo = " + typeof s2.foo);
    
    function test(arg) {
      arg.foo = "bar";
    }
    

    Output:

    [Before] typeof s1.foo = undefined
    [After] typeof s1.foo = undefined
    [Before] typeof s2.foo = undefined
    [After] typeof s2.foo = string

    Note that s2.foo is a string, but s1.foo isn't (because s1 was a string primitive, the object created when we promoted it in test has nothing to do with the calling code).

    Is there any use case for this? Dunno. I'd say it would be an extremely edgy edge case if so.

提交回复
热议问题