Solved

Run variable as a function

Posted on 2014-08-01
2
137 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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…

724 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