A
Asen Bozhilov
I have a simple question. When i have code like this:
var str = 'primitive string value';
str.length;
In ECMA 262-3:
| 11.2.1 Property Accessors
| 1. Evaluate MemberExpression.
| 2. Call GetValue(Result(1)).
| 3. Evaluate Expression.
| 4. Call GetValue(Result(3)).
| 5. Call ToObject(Result(2)).
| 6. Call ToString(Result(4)).
| 7. Return a value of type Reference whose base object is
| Result(5) and whose property name is Result(6).
In the step 5 primitive value will be convert to `object' by
algorithm defined in 9.9 ToObject. In my case will be create
intermediate String object whose [[value]] property is set to the
value of the string.
But what will be happen with relational operators `in' and
`instanceof':
window.alert('string' instanceof String); //false
window.alert('slice' in 'string'); //throw TypeError
Absolutely expected behavior, but my question isn't about that. My
question is, why is that design of `in' and `instanceof' operators.
Why don't coerced primitive values to `object'?
Thanks for responses.
Regards.
var str = 'primitive string value';
str.length;
In ECMA 262-3:
| 11.2.1 Property Accessors
| 1. Evaluate MemberExpression.
| 2. Call GetValue(Result(1)).
| 3. Evaluate Expression.
| 4. Call GetValue(Result(3)).
| 5. Call ToObject(Result(2)).
| 6. Call ToString(Result(4)).
| 7. Return a value of type Reference whose base object is
| Result(5) and whose property name is Result(6).
In the step 5 primitive value will be convert to `object' by
algorithm defined in 9.9 ToObject. In my case will be create
intermediate String object whose [[value]] property is set to the
value of the string.
But what will be happen with relational operators `in' and
`instanceof':
window.alert('string' instanceof String); //false
window.alert('slice' in 'string'); //throw TypeError
Absolutely expected behavior, but my question isn't about that. My
question is, why is that design of `in' and `instanceof' operators.
Why don't coerced primitive values to `object'?
Thanks for responses.
Regards.