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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript String

    How to handle String in Javascript

  • #2
    Regular Coder
    Join Date
    Sep 2011
    Location
    Sweden
    Posts
    154
    Thanks
    1
    Thanked 22 Times in 22 Posts
    Carefully and with love

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Posts
    945
    Thanks
    19
    Thanked 205 Times in 203 Posts
    Quote Originally Posted by Swarup View Post
    How to handle String in Javascript
    Unlike some other programming
    languages strings in javascript
    are objects just like almost everthing
    else is in javascript but they can
    be treated as primitives in many
    situations.

  • #4
    Regular Coder
    Join Date
    Sep 2011
    Location
    Sweden
    Posts
    154
    Thanks
    1
    Thanked 22 Times in 22 Posts
    And there is actually a difference between strings created using a primitive value and strings created using new String.

    Code:
    var a = 'Daddy';
    a.cool = 1;
    
    var b = new String('Daddy');
    b.cool = 1;
    
    alert(a.cool+' '+b.cool)  // result: undefined 1

  • #5
    Regular Coder
    Join Date
    Sep 2011
    Location
    Sweden
    Posts
    154
    Thanks
    1
    Thanked 22 Times in 22 Posts
    And the String.prototype can add new fun functionality to strings:
    Code:
    String.prototype.phpish = function(obj){
      var str = this;
      obj = obj || window;
      str = str.replace(/\$([A-Za-z_0-9]*)/g,'|||$1|||')
      for(var i in obj){
        str = str.split('|||' + i + '|||').join(obj[i]);
      };
      return str
    };
    
    bloke = {
      name: "Earl",
      type: "pearl"
    };
    
    var a = "My name is $name and I'm a $type!".phpish(bloke);
    alert(a);
    Last edited by ironboy; 10-08-2011 at 11:31 PM.

  • #6
    Regular Coder
    Join Date
    Aug 2010
    Posts
    945
    Thanks
    19
    Thanked 205 Times in 203 Posts
    This is a result of
    the toString() method
    in the native code
    being invoked
    in this case ...
    var a = 'Daddy';
    a.cool = 1;

    the a in
    a.cool
    is in fact an object but
    becuase of the native code it
    yeilds a primitive, the underlying
    object is not accessable because
    the toString method is invoked
    here is very similar code

    var b={}
    b.toString().cool = 'string';
    alert(b.cool)

    the toString is automatic in
    var a = 'Daddy';
    a.cool = 1;
    Last edited by DaveyErwin; 10-08-2011 at 11:53 PM.

  • #7
    Regular Coder
    Join Date
    Sep 2011
    Location
    Sweden
    Posts
    154
    Thanks
    1
    Thanked 22 Times in 22 Posts
    Also, strings created as objects are unique instances - strings created as primitives are not. This can be quite useful.

    Proof of concept:
    Code:
    var a = new String('Hello');
    var b = 'Hello';
    var c = 'Hello';
    
    alert((a == b) + ' ' + (b == c))  // true true
    alert((a === b) + ' ' + (b === c))  // false true

    Example of how to use:
    Code:
    var a = new String('Anna');
    var b = new String('Anna');
    var c = new String('Peter');
    c.loves = a;
    
    alert(
      'And there they were ' + a + ' and ' + b + '.'
      + '\n' + c + ' was in love with the ' 
      + (a === c.loves ? 'first one' : '')
      + (b === c.loves ? 'second one' : '')
      + (a !== c.loves && b!== c.loves ? 'none of them' : '')
      + '.'
    );


  •  

    Posting Permissions

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