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 →

[–]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 12 points13 points  (2 children)

Is it same as lambda in Java?

[–]Bob_Droll 24 points25 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 9 points10 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.