0

I have a nested function that I want to call from outside.

var _Config = "";
var tourvar;
function runtour() {
    if (_Config.length != 0) {
        tourvar = $(function () {
            var config = _Config,
                autoplay = false,
                showtime,
                step = 0,
                total_steps = config.length;
            showControls();
            $('#activatetour').live('click', startTour);
            function startTour() {
            }
            function showTooltip() {
            }
        });
    }
}
function proceed() {
    tourvar.showTooltip();
}
$(document).ready(function () {
    runtour();
});

I was hoping to call it by tourvar.showTooltip(); but I seem to be wrong :) How can I make showTooltip() available from outside the function?

4
  • 2
    What is the $ for? is it because you make use of jQuery or some other library? in that case, which library is it? Commented Oct 14, 2013 at 17:48
  • 2
    Just declare the function outside. There are no reasons to declare it inside other function. Commented Oct 14, 2013 at 17:53
  • tourvar.showTooltip = function(){ ... } (although you're going to have to work with native JS objects, not jQuery wrapped ones). Commented Oct 14, 2013 at 17:56
  • FYI: $(function(){}) is shorthand for $(document).ready(function(){});. So, when you do tourvar = $(function(){, you are setting tourvar to $(document). Commented Oct 14, 2013 at 18:07

2 Answers 2

1

since my previous answer was really a hot headed one, I decided to delete it and provide you with another one:

var _Config = "";
var tourvar;

// Module pattern
(function() {
    // private variables
    var _config, autoplay, showtime, step, total_steps; 

    var startTour = function() { };

    var showTooltip = function() { };

    // Tour object constructor
    function Tour(config) {
        _config = config;
        autoplay = false;
        step = 0;
        total_steps = _config.length;

        // Provide the user with the object methods
        this.startTour = startTour;
        this.showTooltip = showTooltip;
    }

    // now you create your tour
if (_Config.length != 0) {
        tourvar = new Tour(_Config);
    }
})();

function proceed() {
    tourvar.showTooltip();
}
$(document).ready(function () {
    runtour();    
});
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your response.... I have made a slight change but get a Tour undefined. I moved if (_Config.length != 0) { tourvar = new Tour(_Config); tourvar.showControls(); } into the function runtour which should kick the tour off and the showcontrols function should show the control. I declared the ShowControls var in function Tour(). Anway, when I runtour() I get Tour is undefined.
sorted it... 99% your code... the only other thing was because I had redacted some... thanks for your help... understand this alot more now.
Glad it helped. Just for others to clarify, the problem was that the Tour() constructor was not visible outside the module. I modified the example and now it should be as easy to use as possible, be sure to take a look: jsfiddle.net/pm9Kb/3
0
function outerFunction() {
    window.mynestedfunction = function() {
    }
}
mynestedfunction();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.