Sometimes older browser might not be able to provide support to newer features. So if we want to use unsupported feature(ex.: Object.create() not supported in older IE versions), we must add this support first. Manually adding support is called Polyfill.

To do so, we we must check, if the feature is supported, and if not, then only add it.

In below example, I will add support for Object.create() for IE(or any browser that does not support it).

So, what happens is, we first check if browser support the object create by running (!Object.create) . If browser doesn’t support it, then we go inside the if statement. Then we add create  variable to Object  which will hold the function. Then we check if we are correctly passing the Object that we need to extend (arguments.length > 1) . Then we create new object F(). Then we define F()’s prototype to be o (which is the object we pass inside). Then we return the F(). To sum up above code, the most useful part is F.prototype = 0; . This is how JS extends the features of one object to another. Just like Java’s inheritance.

Then we can use the Object create as usual, even if the browser doesn’t support this feature.



Leave a Reply

Your email address will not be published. Required fields are marked *