user1467439 user1467439 - 7 months ago 10
Javascript Question

how to target nested expression/function jquery?

I have the following nested function like this:

var initApp = (function(app) {

app.testOne = function () {

if (conditions === true) {
testTwo();
}
console.log("test 1 fired");

var testTwo = function () {
console.log("test 2");
}
}
return app; })({});


I can use "initApp.testOne()" to call testOne but how do I call testTwo() ?

Answer

You are attempting to call testTwo before it is declared:

var initApp = (function (app) {

    app.testOne = function () {

        var testTwo = function () {
            console.log("test 2");
        }
        if (conditions === true) {
            testTwo();
        }
        console.log("test 1 fired");

    }
    return app;
})({});