As far as I understand, every string is an object in Javascript. Still, it “does not work” as I expect it to be:
var a="abc"; //here we get a new string object
a.b = 123; //I seem to declare a property "b" of that object
alert(a.b); //alerts "undefined"
However, if I try to define a string in the “wrong way”, everything works as expected
var a=new String("abc"); //
a.b = 123;
alert(a.b); //alerts "123"
Why is that so?
This happens because the property accessors, (
.and[]) convert the value ToObject.Something equivalent to this happens behind the scenes:
Basically an object is created on the fly, by the property accessor, see the Step 5:
The production
MemberExpression:MemberExpression [ Expression ](orMemberExpression . Identifier) is evaluated as follows:Evaluate
MemberExpression.Call
GetValue(Result(1)).Evaluate Expression.
Call
GetValue(Result(3)).Call
ToObject(Result(2)).Call
ToString(Result(4)).Return a value of type Reference whose base object is Result(5) and whose property name is
Result(6).