Javascript, parameters parameters: the function name as an argument, how to wear a parameter to the function?

The first method: anonymous function

function foo(x) {
   alert(x);
}
function bar(func) {
   func();
}

//alerts "Hello World!" (from within bar AFTER being passed)
bar(function(){ foo("Hello World!") });

The second way: use apply

function eat(food1, food2)
{
    alert("I like to eat " + food1 + " and " + food2 );
}
function myFunc(callback, args)
{
    //do stuff
    //...
    //execute callback when finished
    callback.apply(this, args);
}

//alerts "I like to eat pickles and peanut butter"
myFunc(eat, ["pickles", "peanut butter"]); 

https://stackoverflow.com/a/24091927/3054511

He published 188 original articles · won praise 88 · views 580 000 +

Guess you like

Origin blog.csdn.net/henryhu712/article/details/103237627