Pass a JavaScript function as parameter

前端 未结 13 1702
星月不相逢
星月不相逢 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 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

提交回复
热议问题