Functions in ES6

Claude Pache claude.pache at gmail.com
Mon Mar 25 01:57:23 PDT 2013


Le 25 mars 2013 à 08:28, Axel Rauschmayer <axel at rauschma.de> a écrit :

> 2. Function expressions --> arrow functions

No, it depends. Roughly, I would replace function expressions by arrow functions when I want to use the 'this' binding of the enclosing scope (or don't use the 'this' binding), and the body is short. 

Random counter-example from my ES6 polyfill:

    if (typeof String.prototype.contains !== 'function') {
        String.prototype.contains = function(x, p) {
            return this.indexOf(x, p) !== -1
        }
        Object.defineProperty(String.prototype, 'contains', {enumerable: false})
    }

I could not use an arrow expression (even if it existed in ES5) because I need a proper 'this' binding. In fact, I could hardly use anything else than a function expression.

Another case where I could need a proper 'this' binding in a function expression, is when I register an event listener.

And again, I will probably not use arrow if the body isn't short.

—Claude


More information about the es-discuss mailing list