Link to home
Start Free TrialLog in
Avatar of tonelm54
tonelm54

asked on

Run variable as a function

Im writing my own plugin, and want to be able to run some user-code.

So to simply things, Ive just written a simple POC:-
function tcDialog() {
    var onCloseFunction = function () { alert('hi'); };

    function onClose($tmpFunction) {
        onCloseFunction = $tmpFunction;
    }
    
    function runMe() {
        onCloseFunction();
    }
}

$(document).ready(function () {
    var viewJob = new tcDialog();
    viewJob.onClose(function () { alert ('hi2'); });
    viewJob.runMe();
});

Open in new window

http://jsfiddle.net/HbTpQ/

I have been looking at using the eval function, but have lots of forums saying dont use it, but cant see an alternative.

Anyone available to point me in the right direction of fixing my code to run the function as a variable?

Thank you in advance.
ASKER CERTIFIED SOLUTION
Avatar of Randy Poole
Randy Poole
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rob
you shouldn't use eval.  What Randy has provided will work for you.