Executing a method inside a method

I am currently working on a JavaScript exercise in FreeCodeCamp, and one of the test cases that my code should work with is a function call that looks like this:

addTogether(2)(3);

here is the bare-bones function:

function addTogether() {
  return;
}

When I ran the code below:

function addTogether() {
  return arguments;
}

In the console that the editor provides, I get:

TypeError: addTogether (...) is not a function

The instructions indicate the use of the object arguments, and it works well with function calls of the test case, which have only one argument object (i.e. addTogether(2, 3);), but not the one I showed above.

Is there a way to access / use individual argument objects when they are in the format I showed above?

. - , .

, .

+4
3

. , ( ). JavaScript, , . .

var f = function(y) {
  console.log('f:', y);
};

var getF = function(x) {
  console.log('getF:', x);
  return f;
};

getF(1)(2);
Hide result

, . , , , .

function createGetX(x) {
  return function() {
    // Since x is in the parent scope, we can use it here
    return x;
  }
}

var one = createGetX(1);
console.log(one()); // Always
console.log(one()); // returns
console.log(one()); // one
Hide result
+4

. "addTogether (2)" "(3)".

, , "addTogether (2)" ..., "()", , .

, "addTogether (2)" , . 3 .

, "addTogether" ... , .

, , , ( ) .

+1

You can do this with closures.

function addTogether(x) {    
  return function(y) {
     return x+y;   
  }   
}
Run codeHide result
0
source

Source: https://habr.com/ru/post/1657542/


All Articles