What is JavaScript polyfill?

What is JavaScript polyfill?

 

A polyfill is a piece of code (usually JavaScript on the Web) used to provide modern functionality on older browsers that do not natively support it.

 

Usually poly provides methods and syntax that older browsers does not support. 

For example trim() does not supported by older browsers so the Polyfill  file has code something like that.

 

 

if (typeof String.prototype.trim !== 'function') {
  String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,'')
  }
}

" text ".trim()

 

The reason polyfills are not used exclusively is for better functionality and better performance. Native API implementations can do more and are faster than polyfills. For example, the Object.create polyfill contains only functions that are possible in a non-native implementation of Object.create.


Tags:

Share:

Related posts