I would say that it makes almost no sense, you almost do not care if you are dealing with the primitive string or string .
There are regional cases. For example, a string object is an actual object, you can add properties to it. This allows you to do such things:
function test(arg) { arg.foo = "bar"; }
If the call code passes in the string primitive:
var s1 = "str"; test(s1);
... arg gets the status of string and gets the property added to it, but this string object is not used by anything after test returned.
In contrast, if the call code passes in a string object:
var s2 = new String("str"); test(s2);
... then the property is added to this 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 not (since s1 was a string primitive, the object created when we push it to test has nothing to do with the calling code).
Is there any precedent for this? I do not know. I would say that it will be a very sharp edge, if so.
TJ Crowder Jul 22 '11 at 16:50 2011-07-22 16:50
source share