0

I'm trying to build butter(https://github.com/butterproject/butter-desktop) but it don't compile because a code in hoek library:

/src/butter-desktop/node_modules/hawk/node_modules/hoek/lib/index.js:483
compare = (a, b) => a === b;
                  ^
...
>> Unexpected token >

And there is others lines where this "operator" => is used, like:

    const leftovers = ref.replace(regex, ($0, $1) => {

        const index = values.indexOf($1);
        ++matches[index];
        return '';          // Remove from string
    });

I'm trying to understand, and I guess it's like a "function" operator...

If I got correct is something similar to:

On first code:

compare = (function(a, b) { return a === b; })(a,b);

that in this case is the same as

compare = a === b;

and on second code:

const leftovers = ref.replace(regex, (function($0, $1) {
        const index = values.indexOf($1);
        ++matches[index];
        return '';          // Remove from string
    })($0, $1));

Anyone can confirm and give-me a official reference of this ?

The online code is: https://github.com/hapijs/hoek/blob/master/lib/index.js#L483

ton
  • 3,827
  • 1
  • 42
  • 40

2 Answers2

2

It's an arrow function. Upgrade to Node.js 4.x so that you are able to use ES6 features like this.

JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
2

It is the operator that defines arrow functions, basically a new way to define a function that doesn't create a new child scope...

Nothing new really because you can achieve the same behaviour binding a declared function with its parent scope...

Two examples following:

// ECMASCRIPT 6 ARROW FUNCTION

var fn = () => {
  
  console.log('this is', this);
  
  return true;
}




// ECMASCRIPT 5 ARROW FUNCTION BEHAVIOUR

var fn = function() { 
  console.log('this is', this);
  
  return true;
}.bind(this);
Hitmands
  • 13,491
  • 4
  • 34
  • 69
  • Why do you need to add `.bind(this)` ? is it always 'bound' to this, no ? – Anonymous0day Nov 08 '15 at 19:08
  • Because I want that function work with the context of its parent, in this case, assuming working in a browser, this is equal to window... and isn't binf but bind – Hitmands Nov 08 '15 at 19:09
  • one more thing, you are getting this error because your compiler is not able to transpile ECMASCRIPT 6... have a look on https://babeljs.io – Hitmands Nov 08 '15 at 19:11
  • you wrote: is it always 'bound' to this, no ? No, never, because each new function creates a new child scope... It is how javascript works http://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript – Hitmands Nov 08 '15 at 19:14