Solved

Run variable as a function

Posted on 2014-08-01
2
134 Views
Last Modified: 2014-08-27
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.
0
Comment
Question by:tonelm54
2 Comments
 
LVL 21

Accepted Solution

by:
Randy Poole earned 500 total points
ID: 40233935
function tcDialog() {
    this.onCloseFunction = function () { alert('hi'); },
    this.onClose=function(tmpFunction) {
    this.onCloseFunction = tmpFunction;
    },
    this.runMe=function() {
        this.onCloseFunction();
    }
}

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

Open in new window

0
 
LVL 43

Expert Comment

by:Rob
ID: 40271793
you shouldn't use eval.  What Randy has provided will work for you.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
$q.all is slower or not  in  Angularjs 2 21
jQuery Sticky Menu 24 34
Javascript - Uncaught SyntaxError: Unexpected token } (Image Swap onclick) 12 50
ASP.NET Content Page 3 25
Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question