Jump to content

how to call a function as argument ?


espace
 Share

Recommended Posts

hi,

i'm searching to use this function like this :

var wait=function(callback,duration){
    setTimeout(function(){ callback() }, duration);
}

 

wait(myfunction,100)

but it say callback is not a function...this line is not good interpretated : callback()

how to use a function as an argument ?

 

Link to comment
Share on other sites

Hi @espace,

There is a scope problem. You don't need to put the argument inside an anonymous function. The argument is already a function.

This code works:

var wait = function(callback,duration){
    setTimeout(callback, duration);
 };

var f = function(){
  console.log('executed');
 };

wait(f,500);

Regards.

Link to comment
Share on other sites

Nah, its not even a scope problem, whatever `myFunction` is in the original post, it wasn't a function.

However, I do agree that you don't need to wrap the callback function again, but, you don't even need the `wait` function as it has just become a marginally slower alias. I'm assuming this is all simplified for the purposes of illustration though and that you're actually planning to do more with your wait function (like make it a little easier to cancel it, or extend it, or whatever).

Functions in JS can be passed around as first-class citizens, which is an extremely useful language feature.

function add (a, b) {
  return a + b
}

You can think of the `add` variable as a pointer to a block of code (rather than a pointer to a memory allocation), using newer syntax makes this even more obvious:

const add = (a, b) => a + b

Where you actually assign the anonymous (lambda) function to the constant variable (ha ha, that proper sounds odd!) `add`.

Note: there are subtle differences between the two code snippets above, but you're extremely unlikely to need to worry about it, particularly for pure functions like the ones above.

Either way you can pass the `add` function around, including using it as a parameter for other functions.

Consider the following:

const add = (a, b) => a + b
const subtract = (a, b) => a - b
const d2 = () => Math.random() < 0.5

const tweak2 = (a, b) => d2() ? add(a, b) : subtract(a, b)

Calling the `tweak2` function has a 50/50 chance of either adding or subtracting those numbers, but, imagine we wanted to add other operations:

const add = (a, b) => a + b
const subtract = (a, b) => a - b
const multiply = (a, b) => a * b
const divide = (a, b) => a / b
const d2 = () => Math.random() < 0.5

const tweak2addsubtract = (a, b) => d2() ? add(a, b) : subtract(a, b)
const tweak2addmultiply = (a, b) => d2() ? add(a, b) : multiply(a, b)

This is a pretty terse example but you can imagine how this is going to grow. Now imagine you end up changing some of the implementation, would be pretty error-prone. Imagine trying to test all combos, again, a little annoying.

However, you could refactor this slightly and use another helper function:

const add = (a, b) => a + b
const subtract = (a, b) => a - b
const multiply = (a, b) => a * b
const divide = (a, b) => a / b
const d2 = () => Math.random() < 0.5

const tweak2 = (fnA, fnB) => (a, b) => d2() ? fnA(a, b) : fnB(a, b)

const addSubtract = tweak2(add, subtract)

console.log(addSubtract(10, 2))
// 8 or 12

Now the tweak function can be 'pre-loaded' with functions it uses to operate, which we've passed through.

If you're not comfortable with arrow functions then its a little confusing to follow as it uses closure and explicit return to work, but, have a look as this pattern can be generalised to be useful in a wide variety of situations.

When you get really good you can do stuff like the following:

const isNumber = _ => typeof _ === 'number'
const add = (a, b) => a + b

const total = compose(
  reduce(add, 0),
  filter(isNumber)
)

total([1, 2, 'a', 3])
// 6

All functions used here a low-level pieces and trivial to test and re-compose.

Unless you're comfortable with passing functions around it's probably a little tricky to see exactly what is going on (there are lots of good places to read up on it though) but you should be able to see that the `total` function can take an array, filter out only the numbers, and then add them all up.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...