-3

I have recently come across this code with which I am unfamiliar:

const foo = () => {
    /*code block here*/
}

As far as I can tell, it means the same thing as:

const foo = function () {
    /*code block here*/
}

Is that a correct assumption, or are there differences? What is the correct name to refer to this bit of code? What exactly is the '=>' doing? I've never seen it in Javascript before.

Martha
  • 178
  • 1
  • 11

1 Answers1

1

This is ES6 arrow function. It's basically same as function (){}, with some differences such as not rebinding this.

Reference on MDN

Tatsuyuki Ishi
  • 3,883
  • 3
  • 29
  • 41
  • 2
    All execution contexts have a *this*. Arrow functions adopt the *this* of their enclosing (or "outer") execution context. – RobG Apr 26 '17 at 08:58
  • @RobG the description is basically based on MDN section title, "No binding of this". – Tatsuyuki Ishi Apr 26 '17 at 08:59
  • MDN is not an authority, it's just a (very helpful) public wiki. – RobG Apr 26 '17 at 09:00
  • @RobG I have altered the word to "rebind" so it may be clearer. – Tatsuyuki Ishi Apr 26 '17 at 09:01
  • 2
    [*ECMA-262 ed 7 §14.2.16*](http://ecma-international.org/ecma-262/7.0/index.html#sec-arrow-function-definitions-runtime-semantics-evaluation) says "*An ArrowFunction does not define **local** bindings for arguments, super, this, or new.target. Any reference to arguments, super, this, or new.target within an ArrowFunction must resolve to a binding in a lexically enclosing environment.*" (my emphasis). So guess whether there is a local binding or not is moot, it just must evaluate to the outer value. Saying "no binding" makes it sound like there isn't one at all (IMHO of course). ;-) – RobG Apr 26 '17 at 09:16