Posts

Showing posts with the label polyfills

The JavaScript typeof Operator Problem

TL;DR don't even try to normalize or shim newer typeof via code: you simply can't! Whenever it was a mistake or not to consider typeof null == "object" , many libraries that tried to normalize this operator failed to understand that null is not the only problem . The JS Polymorphism Nature We can borrow methods for basically everything but primitives values, such booleans, numbers, and strings, do not accept indeed any sort of property or method attached runtime. var s = "hello"; s.greetings = true; alert(s.greetings); // "undefined" However, we can still use methods through call() or apply() : function isGreetings() { return /^(?:ciao|hello|hi)$/.test(this); } alert(isGreetings.call("hello")); // true The only way to find a method in a primitive value is to extend its own constructor.prototype : String.prototype.isGreetings = function () { return /^(?:ciao|hello|hi)$/.test(this); }; alert("hello".isGreetings()); // tru...

On Obtrusive Polyfills

Image
I'll try to make the point short and simple, starting with the tl;dr version: if you create a polyfill and during features detection you realize this cannot work, drop it and let other libs deal with it. Broken Object.defineProperty This is just one case really annoying. Bear in mind I am not talking about " making perfect polyfills ", this is almost impossible most of the case, I am talking about broken runtime even if you did not really do anything that special. It was the case of Object.defineProperty shimmed through es5-shim code ... this library is good but that method, as many others, are simply broken. I was trying to add a setter/getter in IE < 9 and shit happened, more specifically a thrown exception happened ... the repository itself explains that this method and others should fail silently ... so it's OK for the developer to know about it and avoid trusting these methods ... This Is Not OK If I check if Object.defineProperty is there and this is al...

ES6 Harmony Collections Fast Polyfill

Well, just in case you read RSS and you missed my tweet ... everything you need to know in github repository . Have fun with Harmony Collections

Partial Polyfills

This is a quick one already discussed during my recent workshop in Warsaw, a concept rarely considered or adopted from JS developers. What Are Polyfills If we are updated enough to know ECMAScript 5th Edition, we probably know all possible shims and fallbacks for Array extras as well (e.g. Array.prototype.forEach ). Polyfills are simply snippets able to bring features already available for most recent browsers into those not updated yet. Why Polyfills If we develop with present and future in mind, we can take advantage of most recent techniques and solutions in order to both speed up via native support, where available, and maintain our application just stripping out code once all target browsers support them natively ... isn't this cool ?! Polyfills Side Effects The most common side effect of a polyfill is performances impact . The Array::forEach is already a good example of decreased performances. If we think about a forEach, it's nothing different than a classic for loop , e...