Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jan 2004
    Location
    USA
    Posts
    364
    Thanks
    12
    Thanked 6 Times in 6 Posts

    Modify string without creating a new variable

    Is this possible?

    Code:
    String.prototype.trim = function(){
    	return this.replace(/^\s+|\s+$/g, "");
    }
    
    var myStr = "  Hi what's up?  ";
    myStr.trim();
    alert("{" + myStr + "}"); //will result in the original string, not the trimmed version
    It's possible with numbers:

    Code:
    var myNum = 10;
    myNum++;
    alert(myNum); //11

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,211
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    Of course.
    Code:
    var myStr = "  Hi what's up?  ";
    myStr = myStr.trim();
    Yeah, I know. It's not the same thing. But it's the best you'll do.

    See, a string variable is just a *REFERENCE* to a string object. And string *objects* are immutable (means they can't be altered).

    So any operation you do on a string *must* return a NEW string, since it's not allowed to touch the original string object.

    But nobody said you can't change *which* string object a given variable references.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    qwertyuiop (07-30-2010)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,211
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    p.s.: You may have asked the question wrong.

    If you had asked
    Can I modify string *OBJECT* without creating a new *OBJECT*?
    the answer is simply "no."
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Regular Coder
    Join Date
    Jan 2004
    Location
    USA
    Posts
    364
    Thanks
    12
    Thanked 6 Times in 6 Posts
    ^wtf? Lots of spam recently...

    Thanks old pendant! I should kick myself... something that simple not coming to my head...

    So then, out of curiosity, numbers are simply mutable objects?

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,211
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    Quote Originally Posted by qwertyuiop View Post
    So then, out of curiosity, numbers are simply mutable objects?
    I think now we are into both implementation and semantics. Not sure.

    I know that in JScript (MS's version of JavaScript), numeric and boolean variables are indeed "mutable", but they aren't truly objects. The variable really is no longer a reference to an object; it carries the numeric/boolean value right inside of itself. (MS uses a COM "Variant" as the root implementation of a variable.)

    But I would *expect* that if you used Number--the object-ized form of a number--then a Number object is immutable. Ditto a Boolean object.

    A cursory glance at the MS JScript docs would seem to confirm this, but I'm not 100% sure.

    How simple variables and values are implemented in other versions of JS I am not sure. (I only know about JScript because I once helped port it to Linux and Unix and IBM mainframes. That was back in JScript version 3.x days.)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •