Why does UnderscoreJS have functions wrapping around a lot of built-in Javascript functions?

I noticed that UnderScoreJS has a lot of wrapper functions around native Javascript functions.

Take for example:

_.isArray, _.isBoolean, _.isNaN?

      

Is there a reason for this? Or are they just meant to provide code consistency when using the underscoreJS library, or are they just to enhance these features?

For example, the _.isArray function jumps to the following:

_.isArray = nativeIsArray || function(obj) {
    return toString.call(obj) === '[object Array]';
  };

      

Any idea?

+3


source to share


1 answer


This is because these features are missing in all browsers. For example try Array.isArray

in IE8 and you won't find it.

Nowadays modern browsers are becoming more and more relevant with the ECMAScript standard, and such "shims" are less and less needed, but this is not always the case!



In most Javascript frameworks, you will find similar seemingly redundant features to ensure that none of their features throws an exception because there is no feature in the given browser.

There are also functions like _.each(obj, func)

that that act without any problem with array-like objects while you need to do Array.prototype.forEach.call(obj, func)

(versus arr.forEach(func)

for a real array). So another benefit is to make sure you forEach

are there first.

+5


source







All Articles