String object vs string value
Hi, I am trying to complete the implementation of the String stdlib functions. I hit a small roadblock that has me puzzled. Consider the following code: // ---- var s = "abc"; function f(x) { if (s === this) print("global = this"); if (x === this) print("arg = this"); if (x === s) print("arg = global"); } String.prototype.f = f; s.f(s); // ---- The code only prints the last string (arg = global) when run with d8. The only explanation I can see is that using a string value (typeof s === 'string') as the receiver of a call (and thus the implicit 'this' parameter) converts it to a string object (typeof this === 'object'). Can somebody point me to the part of the ECMAScript spec that explains this behaviour? I may be blind, but I can't seem to find it... Thanks, Bruno
Afficher les réponses par date
I don't remember the exact part, but you are correct. Strings in JavaScript are not objects, they're primitives... Except that when you try to use them as objects, the spec says a string object should be created on the spot. The result is that you can do: a = "abc"; a.foo = 3; But then a has nothing stored on it, the object used as the field assignment no longer exists after the said assignment... Same for method calls. - Maxime
Hi,
I am trying to complete the implementation of the String stdlib functions. I hit a small roadblock that has me puzzled. Consider the following code:
// ---- var s = "abc";
function f(x) { if (s === this) print("global = this"); if (x === this) print("arg = this"); if (x === s) print("arg = global"); }
String.prototype.f = f; s.f(s); // ----
The code only prints the last string (arg = global) when run with d8. The only explanation I can see is that using a string value (typeof s === 'string') as the receiver of a call (and thus the implicit 'this' parameter) converts it to a string object (typeof this === 'object'). Can somebody point me to the part of the ECMAScript spec that explains this behaviour? I may be blind, but I can't seem to find it...
Thanks,
Bruno_______________________________________________ Tachyon-list mailing list Tachyon-list@iro.umontreal.ca https://webmail.iro.umontreal.ca/mailman/listinfo/tachyon-list
On 2011-02-01, at 18:48 , chevalma@iro.umontreal.ca wrote:
I don't remember the exact part, but you are correct. Strings in JavaScript are not objects, they're primitives... Except that when you try to use them as objects, the spec says a string object should be created on the spot. The result is that you can do:
a = "abc"; a.foo = 3;
But then a has nothing stored on it, the object used as the field assignment no longer exists after the said assignment... Same for method calls.
That makes it clearer. I imagine I can force the conversion with toObject and toString then? Bruno
You can use toString to get the string representation of an object or other primitive, but I don't think there's a toObject method. unfortunately. However, it seems you can do this: d8> a = new String("foo") foo d8> a foo d8> typeof a object d8> - Maxime
On 2011-02-01, at 18:48 , chevalma@iro.umontreal.ca wrote:
I don't remember the exact part, but you are correct. Strings in JavaScript are not objects, they're primitives... Except that when you try to use them as objects, the spec says a string object should be created on the spot. The result is that you can do:
a = "abc"; a.foo = 3;
But then a has nothing stored on it, the object used as the field assignment no longer exists after the said assignment... Same for method calls.
That makes it clearer. I imagine I can force the conversion with toObject and toString then?
Bruno_______________________________________________ Tachyon-list mailing list Tachyon-list@iro.umontreal.ca https://webmail.iro.umontreal.ca/mailman/listinfo/tachyon-list
participants (2)
-
Bruno Dufour -
chevalma@iro.umontreal.ca