Es5-shim and underscore.js?

I have es5-shim.js and underscore.js in my JavaScript project.

es5-shim.js just add some native javascript functions such as reduce and some for arrays for Internet Explorer and some older browsers. underscore.js adds the same thing (but with a different syntax) and much more (utility functions for objects and arrays).

But, if functions added by es5-shim exist, underscore.js are used.

So, in a "recent" browser, such as Firefox or Chrome, underscore.js use their own browser functions. I think this is better than a pure javascript function. But in Internet Explorer, underscore.js uses es5-shim functions. If I remove es5-shim.js, underscore.js uses its own functions.

Any advice on this? Should I remove es5-shim from my project and use only underscore.js? Or should I let underscore.js use es5-shim functions?

+4
source share
2 answers

es5-shim, as you rightly said, adds some JavaScript functions, such as map, reduce, some and forEach on Array, if the JavaScript browser engine is not compatible with ES5. Underscore.js adds utility methods similar to the above (or more) in the namespace "_". Thus, in this case, the use of both in one application is redundant.

Thus, es5-shim adds a few more functions, such as Date.now, Date.toJSON, Function.bind, String.trim (and much more), some of which do not have direct equivalents in Underscore.js. For example, Underscore provides _.bind and _.bindAll, which is equivalent to Function.bind, but Underscore.js does not provide Date.now and Date.toJSON.

So, if you use these additional methods provided by es5-shim, you can stay in your application with both es5-shim and Underscore.js. But if you do not use these additional methods, then using es5-shim will simply be an extra increase in application size and loss of bandwidth.

+4
source

I disagree with @ganeshi's answer.

If your main use for Underscore is things that already come with ES5, such as reduce () and some (), I suggest you remove underscore.js.

If you are now encoding ES5 methods, you can simply remove ES5-shim if you no longer support IE8 or Firefox 3.

If you are using underscores now, then when you no longer support IE8 or Firefox 3, you will still have an extra copy of the large number of things that most browsers already provide.

+10
source

Source: https://habr.com/ru/post/1448087/


All Articles