admin管理员组

文章数量:1400559

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 plicated workaround has been used to replace parts of a string. The ments for the code clearly indicate that it originally used the functional replace() method but that the workaround was necessary for cross-browser patibility.

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 ment 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.

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 plicated workaround has been used to replace parts of a string. The ments for the code clearly indicate that it originally used the functional replace() method but that the workaround was necessary for cross-browser patibility.

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 ment 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.

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Apr 13, 2013 at 11:42 guypurseyguypursey 3,1943 gold badges26 silver badges43 bronze badges 13
  • 3 Or could it be that many users were still using old browsers? – JJJ Commented Apr 13, 2013 at 11:45
  • It's possible. That's what I'm hoping to shed light on. – guypursey Commented Apr 13, 2013 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 plained 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 Commented Apr 13, 2013 at 11:56
  • 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: bugs.webkit/show_bug.cgi?id=3294 so apparently this was fixed sometime at the end of 2005. Based on phrogz/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 patibility with such dinosaurs as Saf 2 or IEMac is important. – kangax Commented Apr 13, 2013 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 that may help. If you post your question on the p.lang.javascript news group (use a news reader for best results) you might get more information. – RobG Commented Apr 14, 2013 at 22:32
 |  Show 8 more ments

1 Answer 1

Reset to default 6

This answer is based on the ments 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 menters on this question.

本文标签: