On Mar 25, 2013, at 1:57 AM, Claude Pache wrote:
>
> Le 25 mars 2013 à 08:28, Axel Rauschmayer <[email protected]> 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.
You could use a concise method:
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
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
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss