Pass a JavaScript function as parameter

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

    I chopped all my hair off with that issue. I couldn't make the examples above working, so I ended like :

    function foo(blabla){
        var func = new Function(blabla);
        func();
    }
    // to call it, I just pass the js function I wanted as a string in the new one...
    foo("alert('test')");
    

    And that's working like a charm ... for what I needed at least. Hope it might help some.

提交回复
热议问题