Pass a JavaScript function as parameter

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

    There is a phrase amongst JavaScript programmers: "Eval is Evil" so try to avoid it at all costs!

    In addition to Steve Fenton's answer, you can also pass functions directly.

    function addContact(entity, refreshFn) {
        refreshFn();
    }
    
    function callAddContact() {
        addContact("entity", function() { DoThis(); });
    }
    

提交回复
热议问题