Re: Are JavaScript strings mutable?
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.javascript archive

Re: Are JavaScript strings mutable?

From: Alexis Nikichine <alexis.nikichine@somedomain.fr>
Date: Thu Apr 20 2006 - 19:03:24 CEST

VK wrote:
> Dr John Stockton wrote:
>
> At the same time I am not sure why Douglas Crockford thinks that
> JavaScript strings are guaranteed to be immutable *everywhere*. The
> specs do not pose any explicit requirements on it, and the issue seems
> more system-dependent rather then language-dependent (?).

Just taking a guess, let's imagine, for instance, that some clever
implementation make use of 'aliasing' for its string implementation (or
even interned string):

   var a, b;
   a = b = "long"; // you might expect that a and b refers
                        // to the "same" string

there's no way you can modify both a and b in a single statement. Let me
try:

   a+= "er";
   alert( a ); // "long"
   alert( b ); // "longer" (same player, shoot again)

So the things behave as if a new string is created out of the one that a
  used to refer too, and the appended one: that new string is then the
one referred to by a. All this because the original, "long" string could
not be mutated into "longer".

Of course implementations are not required internally to alias identical
strings, even in trivial cases, as long as the behavior conforms to the
ECMA standard.

Interestingly, I tried to mutate a String object (not a javascript
string per se) along with all of its alias, but couldn't. It seems ecma
has made enough provision for this "immutability" property to be
preserved for String objects.

Cheers,

Alexis

--
some domain is free
Received on Mon May 1 05:11:05 2006