flasinger
flasinger

Reputation: 31

Is there a static check for native function support across browsers?

I'm wondering if there is a static checker that will look at my javascript sources and tell me which native functions I should not use or use differently if I want my scripts to work across browsers.

For example Chrome does not know String.contains() but Firefox does. I would want to be warned that I should avoid String.contains() completely.

Another example would be the usage of Array.sort(), which only works correctly across browsers if your compare function returns a Number. All browsers will also accept boolean values but not behave consistently in this case. A warning about the return type would be great. (Analysis would be much more complex and maybe not even possible.)

Does somebody know about a static checker that warns about "non-ECMA" usage of native functions. I searched but did not find anything.

Upvotes: 3

Views: 449

Answers (1)

jordiburgos
jordiburgos

Reputation: 6302

What you are looking for it is called a polyfill or polyfiller. It is code that checks for existing behaviors or functions on the browser. If the feature is not present, the polifill implements it.

http://en.wikipedia.org/wiki/Polyfill

An example of this implementation is Modernizr. https://github.com/Modernizr/Modernizr

Example of the String.contains method on https://github.com/robertkowalski/contains.js/blob/master/contains.js

if (typeof String.prototype.contains == 'function') {
  return;
}
String.prototype.contains = function(value, pos) {
   var string = this;
   ... implementation ...
}

Upvotes: 1

Related Questions