Introduction
We have many ways to support writing javascript for older versions of the browser (IE 8, IE 9 …), one of them is to use Polyfill .
What’s a polyfill?
Polyfill is a piece of code that tells the browser how to implement javascript features. Once we have used polyfill for a function, then we will not need to worry about whether the function or method we just wrote will have browser support or not.
Polyfill has a fairly simple method of operation:
- check if the feature has browser support or not
- If the browser does not support, it will add code to support the implementation of the feature
For example, the Array.prototype.find
method, in case the browser does not support Array.prototype.find
, it will instruct the browser how to implement:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | <span class="token keyword">if</span> <span class="token punctuation">(</span> <span class="token operator">!</span> Array <span class="token punctuation">.</span> prototype <span class="token punctuation">.</span> find <span class="token punctuation">)</span> <span class="token punctuation">{</span> Object <span class="token punctuation">.</span> <span class="token function">defineProperty</span> <span class="token punctuation">(</span> Array <span class="token punctuation">.</span> prototype <span class="token punctuation">,</span> <span class="token string">'find'</span> <span class="token punctuation">,</span> <span class="token punctuation">{</span> value <span class="token punctuation">:</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> predicate <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 1. Let O be ? ToObject(this value).</span> <span class="token keyword">if</span> <span class="token punctuation">(</span> <span class="token keyword">this</span> <span class="token operator">-</span> <span class="token keyword">null</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">TypeError</span> <span class="token punctuation">(</span> <span class="token string">'"this" is null or not defined'</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">var</span> o <span class="token operator">=</span> <span class="token function">Object</span> <span class="token punctuation">(</span> <span class="token keyword">this</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token comment">// 2. Let len be ? ToLength(? Get(O, "length")).</span> <span class="token keyword">var</span> len <span class="token operator">=</span> o <span class="token punctuation">.</span> length <span class="token operator">>>></span> <span class="token number">0</span> <span class="token punctuation">;</span> <span class="token comment">// 3. If IsCallable(predicate) is false, throw a TypeError exception.</span> <span class="token keyword">if</span> <span class="token punctuation">(</span> <span class="token keyword">typeof</span> predicate <span class="token operator">!==</span> <span class="token string">'function'</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">TypeError</span> <span class="token punctuation">(</span> <span class="token string">'predicate must be a function'</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">// 4. If thisArg was supplied, let T be thisArg; else let T be undefined.</span> <span class="token keyword">var</span> thisArg <span class="token operator">=</span> arguments <span class="token punctuation">[</span> <span class="token number">1</span> <span class="token punctuation">]</span> <span class="token punctuation">;</span> <span class="token comment">// 5. Let k be 0.</span> <span class="token keyword">var</span> k <span class="token operator">=</span> <span class="token number">0</span> <span class="token punctuation">;</span> <span class="token comment">// 6. Repeat, while k < len</span> <span class="token keyword">while</span> <span class="token punctuation">(</span> k <span class="token operator"><</span> len <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// a. Let Pk be ! ToString(k).</span> <span class="token comment">// b. Let kValue be ? Get(O, Pk).</span> <span class="token comment">// c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).</span> <span class="token comment">// d. If testResult is true, return kValue.</span> <span class="token keyword">var</span> kValue <span class="token operator">=</span> o <span class="token punctuation">[</span> k <span class="token punctuation">]</span> <span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span> predicate <span class="token punctuation">.</span> <span class="token function">call</span> <span class="token punctuation">(</span> thisArg <span class="token punctuation">,</span> kValue <span class="token punctuation">,</span> k <span class="token punctuation">,</span> o <span class="token punctuation">)</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> kValue <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">// e. Increase k by 1.</span> k <span class="token operator">++</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">// 7. Return undefined.</span> <span class="token keyword">return</span> undefined <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">,</span> configurable <span class="token punctuation">:</span> <span class="token boolean">true</span> <span class="token punctuation">,</span> writable <span class="token punctuation">:</span> <span class="token boolean">true</span> <span class="token punctuation">}</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> |
We can easily find this polyfill of Array.prototype.find
above MDN .
Using Polyfills
Basically, there are 2 ways to use polyfill:
- Polyfill manually : manually (same as we did above)
- Polyfill library : add multiple polyfill at a time via library
Polyfilling manually
First we have to search the polyfill we need, maybe via google, now almost every new function or method that older browsers have little support for has polyfill available on MDN .
After finding the appropriate polyfill, we can add it as the above code.
Polyfill library
There are many libraries that contain lots of polyfill, one of which is ES6-shim , it provides all the ES6 polyfill features.
Note polyfill is part of the shim . Shim is a library that provides completely separate APIs for older browsers that do not support the same name APIs.
Using cutting-edge JavaScript features
In addition, we can use Babel – a tool with the main function of compile Javascript, in the process of compile it can read js files, then convert those files to a format that the browser can understand.
What if polyfills aren’t enough?
If in case polyfill is still not enough to support a certain feature, then we should reconsider a little:
- Is it necessary to use the exact same feature for different browsers? We can use one of the other equivalent features but make sure all browsers support it
- Or can we handle it in a different direction without using the feature, or even javascript?
There is no optimal solution, depending on the circumstances, we can use different solutions to best suit.
How to tell if a browser supports the feature?
To check which features we are using are supported by browsers, we can use caniuse.com . Write the javascript feature we want to check, then we can see the result, like with Array.prototype.find
:
Another way we can check on MDN :
Beware the cost of JavaScript
When we use polyfill, it means we will add more lines of javascript code. Once the number of js files is too large, it will also cause many effects:
- Old browsers are often used on older systems, which means downloading large amounts of js files will affect performance.
- The larger the number of js files, the longer the Javascript bundle process will take
Summary
The article is about sharing how to support javascript with old browsers through the use of Polyfill and related libraries or tools. Posts are limited, thank you for taking the time to track.