Allen Wirfs-Brock (2013-03-25T18:09:56.000Z)
github at esdiscuss.org (2013-07-12T02:26:45.036Z)
On Mar 25, 2013, at 1:57 AM, Claude Pache wrote: > > 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: > > ```js > 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. You could use a concise method: ```js if (typeof String.prototype.contains !== 'function') { String.prototype.contains = {contains(x, p) {return this.indexOf(x, p) !== -1}}.contains; } Object.defineProperty(String.prototype, 'contains', {enumerable: false}) ``` or ```js if (typeof String.prototype.contains !== 'function') { Object.assign(String.prototype, {contains(x, p) {return this.indexOf(x, p) !== -1}}); //or Object.mixin } Object.defineProperty(String.prototype, 'contains', {enumerable: false}) ``` Allen