Pass a JavaScript function as parameter

前端 未结 13 1520
星月不相逢
星月不相逢 2020-11-22 07:06

How do I pass a function as a parameter without the function executing in the \"parent\" function or using eval()? (Since I\'ve read that it\'s insecure.)

相关标签:
13条回答
  • 2020-11-22 07:59

    You just need to remove the parenthesis:

    addContact(entityId, refreshContactList);
    

    This then passes the function without executing it first.

    Here is an example:

    function addContact(id, refreshCallback) {
        refreshCallback();
        // You can also pass arguments if you need to
        // refreshCallback(id);
    }
    
    function refreshContactList() {
        alert('Hello World');
    }
    
    addContact(1, refreshContactList);
    
    0 讨论(0)
  • 2020-11-22 08:00

    Example 1:

    funct("z", function (x) { return x; });
    
    function funct(a, foo){
        foo(a) // this will return a
    }
    

    Example 2:

    function foodemo(value){
        return 'hello '+value;
    }
    
    function funct(a, foo){
        alert(foo(a));
    }
    
    //call funct    
    funct('world!',foodemo); //=> 'hello world!'
    

    look at this

    0 讨论(0)
  • 2020-11-22 08:00

    Here it's another approach :

    function a(first,second)    
    {        
    return (second)(first);           
    }     
    
    a('Hello',function(e){alert(e+ ' world!');}); //=> Hello world     
    
    0 讨论(0)
  • 2020-11-22 08:03

    You can use a JSON as well to store and send JS functions.

    Check the following:

    var myJSON = 
    {
        "myFunc1" : function (){
            alert("a");
        }, 
        "myFunc2" : function (functionParameter){
            functionParameter();
        }
    }
    
    
    
    function main(){
        myJSON.myFunc2(myJSON.myFunc1);
    }
    

    This will print 'a'.

    The following has the same effect with the above:

    var myFunc1 = function (){
        alert('a');
    }
    
    var myFunc2 = function (functionParameter){
        functionParameter();
    }
    
    function main(){
        myFunc2(myFunc1);
    }
    

    Which is also has the same effect with the following:

    function myFunc1(){
        alert('a');
    }
    
    
    function myFunc2 (functionParameter){
        functionParameter();
    }
    
    function main(){
        myFunc2(myFunc1);
    }
    

    And a object paradigm using Class as object prototype:

    function Class(){
        this.myFunc1 =  function(msg){
            alert(msg);
        }
    
        this.myFunc2 = function(callBackParameter){
            callBackParameter('message');
        }
    }
    
    
    function main(){    
        var myClass = new Class();  
        myClass.myFunc2(myClass.myFunc1);
    }
    
    0 讨论(0)
  • 2020-11-22 08:04

    If you want to pass a function, just reference it by name without the parentheses:

    function foo(x) {
        alert(x);
    }
    function bar(func) {
        func("Hello World!");
    }
    
    //alerts "Hello World!"
    bar(foo);
    

    But sometimes you might want to pass a function with arguments included, but not have it called until the callback is invoked. To do this, when calling it, just wrap it in an anonymous function, like this:

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

    If you prefer, you could also use the apply function and have a third parameter that is an array of the arguments, like such:

    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"]); 
    
    0 讨论(0)
  • 2020-11-22 08:04

    You can also use eval() to do the same thing.

    //A function to call
    function needToBeCalled(p1, p2)
    {
        alert(p1+"="+p2);
    }
    
    //A function where needToBeCalled passed as an argument with necessary params
    //Here params is comma separated string
    function callAnotherFunction(aFunction, params)
    {
        eval(aFunction + "("+params+")");
    }
    
    //A function Call
    callAnotherFunction("needToBeCalled", "10,20");
    

    That's it. I was also looking for this solution and tried solutions provided in other answers but finally got it work from above example.

    0 讨论(0)
提交回复
热议问题