11

Here is the behaviour I'm looking for:

function one(func){
   func(5);
}

function two(arg1, arg2){
   console.log(arg1);
   console.log(arg2);
}

one(two(3)) //prints 3, 5

Can this behaviour or something similar be accomplished in javascript?

4
  • Can't get the purpose of the functions from the code. It'll better if you can explain it in language. Commented Feb 2, 2017 at 12:36
  • There is no purpose, these are dumbed down examples to illustrate what I am looking for without unnecessary details Commented Feb 2, 2017 at 12:37
  • 1
    I think @MrFarberToYou's answer sums up my thought as well. Commented Feb 2, 2017 at 12:40
  • That behaviour is impossible as is; you're only passing one argument to two yet are immediately trying to log the value of two arguments, at least one of which cannot exist. Commented Feb 2, 2017 at 12:42

7 Answers 7

11

You can always use the bind() function to pass some arguments to your function. It'll create a new function with the first argument - arg1 - equal to the value of 3 in this example:

function one(func){
   func(5);
}

function two(arg1, arg2){
   console.log(arg1);
   console.log(arg2);
}

one(two.bind(null, 3))

You can read more about the bind() function here: MDN - Bind

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

Comments

7

Some workaround is possible

function one() {

  var args = Array.prototype.slice.call(arguments);
  var func = args[0];
  args.splice(0, 1);
  args.push(5);
  func.apply(this, args);
}

function two(arg1, arg2) {
  console.log(arg1);
  console.log(arg2);
}

one(two, 3)

Comments

2

There's a problem with your syntax: function one is expecting its single argument to be a function. Then, below, when you invoke it, you are not passing the function two, but whatever two returns when it's passed a single argument, probably undefined. I don't know what specifically you're trying to accomplish but I'd recommend a little research into closures.

1 Comment

if two(3) call returns a function then one is receiving function as argument.
0
function one(arg){
   two(arg, 5); // func here is two so it requires two params...
}

function two(arg1, arg2){
   console.log(arg1);
   console.log(arg2);
}

one(3)// one expect function so can't execute function here!

1 Comment

Not really what I was looking for, the idea is the function one doesn't know what the first argument to be passed is before the function is called
0

as soon as one expects function as argument two(3) should return function.
this condition is required so in order to achieve it your two function should be

function two(arg1){
    console.log(arg1);
        return function(arg2) {
        console.log(arg2);
    };
}

so two(3) function call gets passed as argument to one
so before assigning value to variable engine executes it. And execution of two(3) call logs 3 to console and returns function

   function(arg2) {
      console.log(arg2);
    };

and then engine assigns executed value(returned function) to func variable. so func parameter of one function now looks like

func = function(arg2) {
    console.log(arg2);
 };

one calls func with 5 passed in as argument. so 5 gets logged to console.

Comments

0

Basically you can't specify the parameter in the function or it'll run. You need to specify the function aka one(two), but that obviously wouldn't work.

However if you dynamically create a function you should be able to accomplish the task like so :

function one(func){
   func(5);
}

function two(arg1, arg2){
   console.log(arg1);
   console.log(arg2);
}

one(function(val) { two(3, val) }) //prints 3, 5

Comments

0

well somehow this things work for me

function one(func){
  console.log("one is running");
  func;
}

function two(args1, args2){
  console.log("two is running");
  console.log("args1 -> " + args1);
  console.log("args2 -> " + args2);
}

//to call it
args1 = 6;
args2 = 12
one(two(args1,args2));

Comments

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.