Solved

Run variable as a function

Posted on 2014-08-01
2
131 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 42

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

863 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now