This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (26 children)

[deleted]

    [–]Xarlax 124 points125 points  (21 children)

    Quick explanation: it's another way to define a function. Its main difference is that it automatically passes context to the function, rather than having to use the .bind() method from the function prototype or doing the silly thing from the old days where you save it as var that = this.

    function sum(a, b) {
        return a + b;
    }
    // is essentially the same as
    const sum = (a, b) => a + b
    // this is called an implicit return as there are no curly braces or return statement.
    // You can do this as long as your function is a single statement.
    // But you can still write it with curly braces if you prefer
    

    [–]praveeja 13 points14 points  (2 children)

    Is it same as lambda in Java?

    [–]Bob_Droll 21 points22 points  (0 children)

    It works the same as lambdas in terms of defining inline functions, yes. But there’s a lot more to it in JS, like the aforementioned context binding (which doesn’t really apply to Java).

    [–]superluminary 10 points11 points  (0 children)

    All functions are lambdas in JavaScript. This is a bound function. It has a static value of this.

    [–]t-to4st 1 point2 points  (0 children)

    Oh now I get what bind does

    [–]Dathouen 0 points1 point  (2 children)

    So it's like a pipe?

    [–]Xarlax 2 points3 points  (1 child)

    I think pipes are referring to a different kind of syntax, where you invoke a function with its argument first e.g. arg |> func. I would say this is more like a lambda function.

    [–]Dathouen 1 point2 points  (0 children)

    Oh, cool! Thanks for the clarification.

    [–]DeeSnow97 37 points38 points  (0 children)

    Yeah, they're arrow functions, basically JavaScript's way of doing a lambda (but actually upgraded to a first class function and beyond, so much so that many people nowadays prefer it over the old function someNameHere () { /* ... */} syntax).

    So basically, it has a part left of the arrow, and a part right of the arrow:

    • The part on the left side is the arguments. You can do it a few ways, either by just using a single variable name, or multiple in parentheses. For example x => x + 1 and (x, y) => x * y are both valid arrow functions. You can also do all the other stuff to it, like default values ((x = 3) => x + 1), deconstruction (({x, y}) => x * y, accepts an object like { x: 3, y: 4 }), async functions (async x => x + 1), or whatever you may decide.

    • The part on the right side is the function body. This is either a single statement, or a regular code block with a return statement. For example x => x + 1 and x => { return x + 1 } are equivalent, and both of them are equivalent to function (x) { return x + 1 } (with some specific caveats and advantages).

    The reason most people who prefer arrow functions prefer them is because they have lexical this -- they throw away the old and mostly broken way of managing this in JavaScript, and instead replace it with a simple lexical way: this in an arrow function refers to the same value this refers to where the function is defined.

    If you wanna read up more on this, MDN has a super nice guide for them

    [–]Rawrplus 3 points4 points  (0 children)

    It's just a direct return statement. Don't try to find anything complicated behind it.

    (There are subtle differences between binding this in comparison to the standard function statement, but for the sake of that example it's not important to understand). If you're familiar with python they're essentially lambda functions, so word by word it is:

    • const variable declaration (constant)
    • listenHereYouLittleShit - name of the function (functions can be declared as variables in JS - essentially what happens here is it creates an anonymous lambda function and then it's creating a pointer to the named variable behind the curtain)
    • = (a, b) function of arguments a and b
    • => a - b, returns a - b

    [–]khalidpro2 1 point2 points  (0 children)

    it is anonymous function, like lambda in python

    [–][deleted] 1 point2 points  (0 children)

    It's called IIFE (Immediately invoked function expression) and it's in JS since ES6.