Javascript:如何从方法中的函数获取值

时间:2016-12-19 19:15:56

标签: javascript function sqlite methods nested

我试图从方法中的嵌套函数中获取值(true或false),但值返回UNDEFINED。如何从对象嵌套函数中获取值?

var result = { 

        compute: function() {
            this.retorno = result.transaction();
        },

        transaction: function() {

            var db = window.sqlitePlugin.openDatabase({name: 'database.db', location: 'default'}); 

            db.transaction(checaFR,erroFR); //Check if (F)irst (R)un.                                

            function checaFR(tx){           //Check if table exists
                tx.executeSql('SELECT * FROM setup',[],checaFRSuccess,erroFR2);
                alert("Select Query OK");

            }

            function erroFR(err){           //Return if error
                alert('Ops - '+err);
                return false;               //This value I Need! :(
            }

            function checaFRSuccess(tx,result){
                alert("Query Sucess "+result.rows.length);
                return ('Rows: '+result.rows.length);
            }

            function erroFR2(err2) {        //If no DB table

                alert("erroFR2: "+JSON.stringify(err2));
                db.transaction(populateDB, errorCB, successCB);  //Start DB Populate
            }

            function populateDB(tx) {
                tx.executeSql('CREATE TABLE IF NOT EXISTS setup (id INTEGER PRIMARY KEY AUTOINCREMENT, runned TEXT NOT NULL)');
                tx.executeSql('INSERT INTO setup(runned) VALUES ("1")');
            }

            function errorCB(errCB) {
                alert("Error processing SQL: "+errCB);
                return false;                  //This value I Need! :(
            }

            function successCB() {
                alert("successCB OK");
                return true;                   //This value I Need! :(
            }

        }
    };  
    result.compute();                          //Start the main function

return {
    retorno: "var: "+result.retorno      //This returns UNDEFINED
     };

如何从此函数中获取值并传递它?

注意:所有回调和警报都在起作用,包括数据库创建。 :D

2 个答案:

答案 0 :(得分:1)

您可以使用以下代码参考嵌套在里面的函数。

result.transaction().erroFR();
result.transaction().errorCB();

之前需要从内部函数返回对象,需要公开访问。这基本上是一个模块模式,您只能将相关信息暴露给外界。因此,您需要公开函数,如下所示。请注意返回对象的函数末尾的return语句。

var result = { 

        compute: function() {
            this.retorno = result.transaction();
        },

        transaction: function() {

            var db = window.sqlitePlugin.openDatabase({name: 'database.db', location: 'default'}); 

            db.transaction(checaFR,erroFR); //Check if (F)irst (R)un.                                

            function checaFR(tx){           //Check if table exists
                tx.executeSql('SELECT * FROM setup',[],checaFRSuccess,erroFR2);
                alert("Select Query OK");

            }

            function erroFR(err){           //Return if error
                alert('Ops - '+err);
                return false;               //This value I Need! :(
            }

            function checaFRSuccess(tx,result){
                alert("Query Sucess "+result.rows.length);
                return ('Rows: '+result.rows.length);
            }

            function erroFR2(err2) {        //If no DB table

                alert("erroFR2: "+JSON.stringify(err2));
                db.transaction(populateDB, errorCB, successCB);  //Start DB Populate
            }

            function populateDB(tx) {
                tx.executeSql('CREATE TABLE IF NOT EXISTS setup (id INTEGER PRIMARY KEY AUTOINCREMENT, runned TEXT NOT NULL)');
                tx.executeSql('INSERT INTO setup(runned) VALUES ("1")');
            }

            function errorCB(errCB) {
                alert("Error processing SQL: "+errCB);
                return false;                  //This value I Need! :(
            }

            function successCB() {
                alert("successCB OK");
                return true;                   //This value I Need! :(
            }

            // Return an object containing functions which needs to be exposed publicly
            return {
               checaFR : checaFR,
               erroFR : erroFR,
               checaFRSuccess : checaFRSuccess,
               erroFR2 : erroFR2,
               populateDB : populateDB,
               errorCB : errorCB,
               successCB : successCB
            }

        }
    }; 

答案 1 :(得分:0)

您需要将回调函数传递给result.transaction方法,并且false值将传递到该回调函数。

你必须这样做bc false是在db.transaction期间/之后派生的,这是一种异步方法...所以我们也必须异步传递false