Functions in ES6

Axel Rauschmayer axel at rauschma.de
Mon Mar 25 11:17:38 PDT 2013


Possibly worth mentioning: If you use `super` then Object.defineProperty() does not work, only Object.assign() and Object.mixin() update [[HomeObject]]. AFAIK, we don’t have Object.defineMethod() yet (maybe ever).

On Mar 25, 2013, at 19:09 , Allen Wirfs-Brock <allen at wirfs-brock.com> wrote:

> 
> 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:
>> 
>>   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
> 
> 

-- 
Dr. Axel Rauschmayer
axel at rauschma.de

home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130325/8808ab2a/attachment.html>


More information about the es-discuss mailing list