0

I am working on a code snippet that is something like nested functiones inside again and again in JavaScript and when I am calling the most outer function then I want the value returning from the inner function as below.

function BooleanFunc(par) {
    function(){
        function(){
            if(par === 0){              
                return true;
            }
            else {
                return false;
            }
        };      
    };
};

if(BooleanFunc(0)){
    alert("Returning True...");
}
else {
    alert("Returning False...");
}

To work on easy, here is the FIDDLE of this code. Me using the Pure JavaScript. There are many solution available but those are for that nested functions that have some names but in my case, inner function have no name and I cant change the functions. So How to return the Value from nested functions...???

UPDATED:

I am working on a code that is using the WebSQL. From the below code, I am searching my player in the database and i its found there then it will return TRUE or else it will return FALSE. But its not working...

/* SEARCH
----------------------------------------------- */
function searchPlayer(player) {
    // Check To Ensure That mydb Object Has Been Created
    if (mydb) {
        // Select The Matched Query
        mydb.transaction(function (t) {
            t.executeSql("SELECT COUNT(*) AS c FROM myTable WHERE playername = ?", [player], 
                function(t,results){
                    if(results.rows[0].c > 0){
                        return true;
                    }
                    else {
                        return false;
                    }
                },
                function(t,error){
                    alert("Error: " + error.message);
                }
            );
        });        
    }
    else {
        alert("Database Not Found, Your Browser Does Not Support WebSQL...!!!");
    }
}

SOLUTION:

The nested function is taking time to load and reply so I just gave some time difference between both sets of codes so after this second set of code is working fine on reply of first set of code.

Muhammad Hassan
  • 1,224
  • 5
  • 31
  • 51
  • You need to call inner function. – Tushar Jan 03 '16 at 12:56
  • Your code never calls the inner function. Perhaps you can expand your example, or explain what you want to do. – David Goldfarb Jan 03 '16 at 12:56
  • @Tushar How can I call inner function...??? – Muhammad Hassan Jan 03 '16 at 13:01
  • @DavidGoldfarb Here I added my real codes. – Muhammad Hassan Jan 03 '16 at 13:01
  • @Niet the Dark Absol Before marking as duplicate, try to read the question once. Having same keywords doesn't make sense to be duplicate. Mine question is totally different. I am not making any AJAX. If you have no answer, you are allowed to close the page. I will welcome you most. – Muhammad Hassan Jan 03 '16 at 13:18
  • The question is the same principle: returning a value from a callback. Whether it's AJAX or a database engine, it doesn't matter. – Niet the Dark Absol Jan 03 '16 at 13:20
  • Principle and Logic are two different things. You can better explain via login besides paragraphs... – Muhammad Hassan Jan 03 '16 at 13:31
  • The literal answer to your question is that it can't be done. There is no way to get a value, via **return**, from an inner function that is neither called nor returned from the outer function. (Well, to be pedantic, there is no _reasonable_ way to do so). You can do either of two things: 1) Change the outer function to return the inner. You can then call outer to get inner and then call inner. This will come closer to answering your literal question, but is almost certainly not what you want. Or, – David Goldfarb Jan 04 '16 at 21:17
  • 2) Change the inner function so that instead of **return** ing a value, it calls a callback function to pass out a a result. You can do this in either of two ways: a) pass in the callback function as an argument to the inner function; or b) define the function in an outer scope This approach (callbacks) is especially important because your API (AJAX or WebSQL, or ...) is probably async. It does not call the inner function until long after your function has exited. By then, it is too late to return a value. You need to return results by callback. – David Goldfarb Jan 04 '16 at 21:24
  • @DavidGoldfarb Now this is the real answer. Thanks for explaining. Now I got it by updating a global variable instead of returning and later using that variable value and I got it solved. Please edit the question add add your comment there so others can see the answer there easily... – Muhammad Hassan Jan 06 '16 at 07:25

0 Answers0