14

According to the 6th Edition of JavaScript: The Definitive Guide (Flanagan, 2011):

ECMAScript v3 specifies that the replacement argument to replace() may be a function instead of a string.

I'm looking at some code written in 2005, where a complicated workaround has been used to replace parts of a string. The comments for the code clearly indicate that it originally used the functional replace() method but that the workaround was necessary for cross-browser compatibility.

ECMAScript v3 came out in 1999 and, as far as I can tell (from this discussion post and this blog post), ECMAScript v3 was supported across the main browsers since late 2001. Could the author have been wrong, or can someone shed light on why such a workaround might have been necessary in 2005?

...

UPDATE The actual comment by the author of the code says:

lambda functions in RegExps are currently a problem with too many browsers.

changed code to work around.

The author code works for or runs this business, though the code itself may well be a personal project.

Community
  • 1
  • 1
guypursey
  • 3,114
  • 3
  • 24
  • 42
  • 3
    Or could it be that many users were still using old browsers? – JJJ Apr 13 '13 at 11:45
  • It's possible. That's what I'm hoping to shed light on. – guypursey Apr 13 '13 at 11:46
  • 2
    I think Juhana's probably right - it all depends on what the code in question was used for. Maybe enough of its intended audience was running outdated browsers; perhaps just a single person with an outdated setup complained loudly enough. Maybe it was targeting some non-desktop-class browsers that were behind the "main" browsers. Kinda hard to tell without knowing the context – Flambino Apr 13 '13 at 11:56
  • Thanks @Flambino. I think I've provided all the relevant context I can. I didn't want to include the workaround code as it's not pertinent to the question (though I did link to question in which I ask about a version of the workaround). I was hoping someone with a good memory or with a better set of bookmarks than mine on the history of JavaScript and browser usage would be able to point me in the right direction :-) – guypursey Apr 13 '13 at 12:03
  • Of course, but don't expect a definitive answer. As you say yourself, the workaround is there to support uncommon (even for 2005) browsers, but that it was deemed necessary to support these was a judgement call. If it's not spelled out in a comment or commit message, and you can't ask the author, we'll probably never know. And by "context", I meant whether the code's part of a little personal site, a big corporate system, a code library, or something else entirely. Each context has different compatibility requirements/tolerances. – Flambino Apr 13 '13 at 12:21
  • Thanks again @Flambino. I didn't say the workaround is there to support 'uncommon browsers'. I have appended the author's comments to my question for context; the comments simply say 'with too many browsers'. That's what made me curious; to my knowledge, *many* browsers were supporting the functionality he mentions. I guess we will never know if the author was wrong as such, but I thought someone might come out and say 'the workaround was needed because in 2005, usage of was prevalent' or 'though the ECMAScript v3 was nominally supported, some of the implementation was buggy'. – guypursey Apr 13 '13 at 12:36
  • I seem to recall a time when not all did support this, but it has been a while. It seems that even MDN said so. I just don't remember what browsers, or, if they supported that particular replace method, what the bugs were. – JayC Apr 13 '13 at 14:32
  • 4
    From what I remember, Safari 2.x didn't support `replace` with function as a 2nd argument. This is partially why in Prototype.js we used `String#gsub` (aside from rubyesque method name and some additional sugar). Quick google search shows this webkit bug: https://bugs.webkit.org/show_bug.cgi?id=3294 so apparently this was fixed sometime at the end of 2005. Based on http://phrogz.net/JS/replaceTest.html apparently IEMac didn't support this either :) All of this means that you can safely use function-as-2nd-argument today, unless compatibility with such dinosaurs as Saf 2 or IEMac is important. – kangax Apr 13 '13 at 18:43
  • 2
    You may be reading too much into this. "Too many" might have been 1% of traffic, you can't tell without context. If you want more info, there's an [ECMAScript support matrix](http://pointedears.de/es-matrix) that may help. If you post your question on the [comp.lang.javascript](https://groups.google.com/forum/?fromgroups#!forum/comp.lang.javascript) news group (use a news reader for best results) you might get more information. – RobG Apr 14 '13 at 22:32
  • @JayC Thanks. I did have a good trawl through the MDN pages. If you can remember or come across one that's relevant, please share :-) – guypursey Apr 15 '13 at 16:50
  • @kangax +1 Thank you; that's as good as answered my question! Even the workaround suggested on the Webkit page is basically the one I've been looking at. I've altered the grammar in some of my question to take on board your point about it being safe to use these days (I had assumed that but perhaps it wasn't clear before). If you want to post what you said as an answer, I'd be happy to accept it :-) – guypursey Apr 15 '13 at 16:56
  • @RobG Thank you for the link to the support matrix. That's really helpful; I'll be bookmarking that for future use. I think kangax has answered my question now but I may go to the news group if I need more details or have a similar question in future. Thanks :-) – guypursey Apr 15 '13 at 16:59
  • Thanks to all for help with this question. I have now posted an answer which I hope summarises the discussion here and includes a bit more information. I have accepted my own answer for the sake of neatness but would be happy to upvote (possibly even accept) other answers if appropriate. – guypursey May 07 '13 at 11:52

1 Answers1

6

This answer is based on the comments to the question above (with special thanks to kangax, whose answer I will likely accept if he chooses to leave one!)

Whilst it's possible that I could have been reading "too many browsers" literally, it's also possible that String.replace() with a function as the argument was a problem in Safari 2.x (using JavaScriptCore) and in IEMac 5.x in the year 2005. Evidence of these problems in that time exists here, and with those particular versions of the aformentioned browsers here.

In fact the workaround mentioned by Gavin Kistner on the first of those pages is one whose performance may be better in some browsers than the functional replace method, as discussed here.

Nevertheless, performance aside, it seems (as I suspected) that a functional replace is acceptable in all browsers today.

Many thanks to all commenters on this question.

Community
  • 1
  • 1
guypursey
  • 3,114
  • 3
  • 24
  • 42