String.prototype.replace() problems with JSON.stringify() and serialization of Objects

Alex Kit alex.kit at atmajs.com
Tue Jul 29 18:07:12 PDT 2014


ES6 template string already solve the problem that this thread states, without changing replace

with the link it was alright
The problem here is that a template string is the literal, that means you have to hardcode it. But more interesting and more usual are dynamic strings(variables): var x = title.replace('foo', data);.
And one more - template strings are just sugar for the concatenation, and the subject is about the replacements. 
​
- Alex

From: waldron.rick at gmail.com
Date: Tue, 29 Jul 2014 16:32:32 -0700
Subject: Re: String.prototype.replace() problems with JSON.stringify() and serialization of Objects
To: alex.kit at atmajs.com
CC: f.bagnardi at gmail.com; es-discuss at mozilla.org



On Tue, Jul 29, 2014 at 4:24 PM, Rick Waldron <waldron.rick at gmail.com> wrote:


ES6 template string already solve the problem that this thread states, without changing replace: http://gul.ly/149g


Sorry, I should've just included this in-line: 

  var func = function() {    var y = [ 52, '$'];    return null;

  };
  var data = func.toString();  var x = `This is a simple ${data}`;
  console.log(x); 

Result: 


  This is a simple function () {    var y = [52, '$'];    return null;  }





Rick





On Tue, Jul 29, 2014 at 2:24 PM, Alex Kit <alex.kit at atmajs.com> wrote:






Would be nice to use something similar that mozilla now does: replace(string, mix, ?flags), but so that flags are also applied for strings. It wont break nothing as when (string, string) is used, then usual behaviour is applied, but with flags the behaviour is extendible, it would be possible then:



// replace global
'foo x foo'.replace('foo', 'baz', 'g') //> 'baz x baz'

// global and case insensitive
'Foo x foo'.replace('foo', 'baz', 'gi') //> 'baz x baz'

// and now the subject, e.g. `r`(raw) flag
'foo bar`'.replace('foo', '$`', 'rg') //> '$` bar`'

​

From: f.bagnardi at gmail.com


Date: Tue, 29 Jul 2014 14:00:22 -0700

Subject: Re: String.prototype.replace() problems with JSON.stringify() and	serialization of Objects
To: waldron.rick at gmail.com
CC: es-discuss at mozilla.org




Of course it's a hack :-)
So, moving forward, it'd have to be an extra method on strings, that only provides a small change in behavior.  It's probably not going to happen because it's such a small change.  





Potentially a S.p.replaceAll which has the same behavior as split-join in that: "a b a c a d".replaceAll("a", "X") => "X b X c X d".





But then you end up with similar concerns to those expressed in the current .contains thread (it's inconsistent).



On Tue, Jul 29, 2014 at 1:32 PM, Rick Waldron <waldron.rick at gmail.com> wrote:








On Mon, Jul 28, 2014 at 10:29 PM, Christoph Martens <cmartensms at gmail.com> wrote:








  
    
  
  
    On 28.07.2014 17:24, Rick Waldron
      wrote:

    
    
      

        

          

          On Mon, Jul 28, 2014 at 11:16 AM,
            Boris Zbarsky <bzbarsky at mit.edu>
            wrote:

            
              On 7/28/14, 11:09 AM, Rick Waldron wrote:

                 
                   var y = x.replace('{{blob}}', function() {

                       return data;

                     });

                
                

              
              In fairness, that's the sort of thing that gives off a
              "WAT smell". Code like this without a comment that
              explains the indirection is just asking someone to
              "simplify" it, breaking it in the process...
            

            
            I don't disagree with your feedback, but JS has had
              special semantics for "$" (with ’ or n) character in the
              replaceValue since ES3 (just short of 15 years). I didn't
              say that the solution was obvious, just that it required
              familiarity ;)
            

            
            Rick

            
          
        
      
      

      
      

      _______________________________________________
es-discuss mailing list
es-discuss at mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

    
    

    Hey Rick,

    

    To be honest, I didn't know the trick with the dummy filter function
    returning the plain data. I expected that it was executed each time
    a match was found (similar to Array filter functions).

    

    I think for external guys writing JavaScript, the WAT effect is
    pretty huge. Either we should change the behaviour of the parameters
    (which is bad for legacy implementations)
The progress of JS is limited only to non-web-breaking changes. 



Rick 



_______________________________________________

es-discuss mailing list

es-discuss at mozilla.org

https://mail.mozilla.org/listinfo/es-discuss





_______________________________________________
es-discuss mailing list
es-discuss at mozilla.org
https://mail.mozilla.org/listinfo/es-discuss 		 	   		  



 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140730/9ab15566/attachment.html>


More information about the es-discuss mailing list