2

I have found a script example, in it there is a line of code that looks something like this:

fn = (arg1) => (arg2) => {
    //do something with arg1 and arg2
}

I am wondering exactly what is happening here, and how would it look as a "normal" function?

1

3 Answers 3

6
fn = (arg1) => (arg2) => {
    //do something with arg1 and arg2
}

fn is a name for the first anon function its basically a function that returns another function

it translated roughly to

var fn = function(arg1){
  return function(arg2){
    ... // do something 
  }
}

noting that the this value will differ because it is an arrow function .

Sign up to request clarification or add additional context in comments.

5 Comments

tx, but what is it that happens with this?
arrow function does not bind its own this unlike regular functions .
so, in the first example thiswould refer to fn, whereas thisinside the return function in the second example will refer to the return function itself?
regular functions in javascript introduce their own this depending on how they are called , but arrow functions don't , if you call this inside an arrow function it will treat it as a regular variable and go look for in outer scope .
6

It looks like two nested function, where the outer function returns the inner function with a closure over arg1.

var fn = function (arg1) {
        return function (arg2) {
            //do something with arg1 and arg2
        };
    };

var fn = function (arg1) {
        return function (arg2) {
            return arg1 + arg2;
        };
    };


var add4 = fn(4),
    add20 = fn(20);

console.log(add4(5));  //  9
console.log(add20(5)); // 25

Arrow function:

An arrow function expression has a shorter syntax than a function expression and does not bind its own thisargumentssuper, or new.target. These function expressions are best suited for non-method functions, and they cannot be used as constructors.

2 Comments

nice example, but the other answer mentions some difference in this, do you know what it means?
@Winter Out of scope for this question / comments. Give this question a read.
-1

I cannot add comments so I write this as an answer. Your example is also known as currying a concept that allows you to pass in a subset of arguments to a function and get a function back that’s waiting for the rest of the arguments.

8 Comments

If you don't have enough rep to add comments, you need to gain rep by adding valid answers. In fact, if you just added some example code to answer the OP's question this would be a perfectly good answer
exactly that is currying .
@CodingIntrigue Yea I've figured that out, but what I dont understand is why I get -1 when I give valid answers. Its wierd? When do you normally use let fn = (arg1) => (arg2)? If you know about the currying concept, it is easier for you to understand the example code, isnt it?
People feel that this doesn't quite answer the question. The question is I am wondering exactly what is happening here, and how would it look as a "normal" function? If you asked me that and I said it's also known as currying I'm pretty sure you'd give me a quizical look :) It's certainly correct, it's just it is a footnote to the actual answer - you should look at the upvoted answers here so see how they differ.
CodingIntrigue yes and that is exactly why I wrote 'I cannot add comments...', because my answer is a sidenote, so people who is reading this question gets to know about the concept and can understand the 'what is happening here' part in the question.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.