4

Say I have code like this:

function Foo(func) 
{
    var a = new SomeClass(func(5));
}
var b = new Foo(x=>x);

What does the x => x mean in the parameter? x is not defined anywhere else.

BlueTasted
  • 41
  • 2
  • 3
    Its the function passed as an argument. The function accepts the parameter `x` and returns it back. Also if `x` is not declared(its value can be `undefined`) anywhere there will be a runtime exception. – Hozefa Sep 17 '18 at 04:04
  • Foo() doesn't return anything, and x doesn't appear anywhere else in the script and yet the code executes correctly. – BlueTasted Sep 17 '18 at 04:08
  • Not my solution but the answer is given in the below link [Javascript code trick :What's the value of foo.x](https://stackoverflow.com/questions/32342809/javascript-code-trick-whats-the-value-of-foo-x) – Redous Sep 17 '18 at 04:08
  • This is called "arrow functions". Read more about it here: http://www.ecma-international.org/ecma-262/6.0/#sec-arrow-function-definitions – Pedreiro Sep 17 '18 at 04:39

2 Answers2

2

It is the arrow notation,

x=>x

implies function which takes one parameter and returns back the same parameter.

It is the same as:-

function test(x) {
  return x;
}
var b = new Foo(test);
Gaurav joshi
  • 1,743
  • 1
  • 14
  • 28
0

As @Hozefa said, the function accepts the parameter x and returns it back.

Basically:

const func = x => x

means:

const func = function (x) {
    return x
}

It's an ES6 Syntax which you learn more here: http://es6-features.org/

Axel
  • 4,365
  • 11
  • 63
  • 122