Solved

Run variable as a function

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
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…

743 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

13 Experts available now in Live!

Get 1:1 Help Now