0

I am learning javascript, i was wondering if it was possible to have something like this use of lambda function

function operation (function f, num1, num2){
    f.call(num1, num2);
}
operation((a,b)=>{return a+b}, 2,3);

I have Unexpected token function at line 1, for function f i imagine, is there a way to make this work somehow ?

1
  • In javascript not exist types! replace function f by df Commented Jul 13, 2016 at 20:34

3 Answers 3

3

Try this:

function operation (f, num1, num2){
    f(num1, num2);
}
operation((a,b)=>{return a+b}, 2,3);

JavaScript is untyped language. If you want to use call, try this:

function operation (f, num1, num2){
    f.call(this, num1, num2);
}
operation((a,b)=>{return a+b}, 2,3);
Sign up to request clarification or add additional context in comments.

Comments

1

JavaScript function argument definitions do not have types. Remove the keyword function from just before the f on line 1.

Comments

0

Yeah, just remove the function:

function operation (function f, num1, num2){
  f.call(num1, num2);
}
operation((a,b)=>{return a+b}, 2,3);

however this will call your function with the this-context num1, and the first parameter num2. If you want to call the function without overriding the this context just call it with ():

f(num1, num2)

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.