0

我在使用以下 Javascript 代码(Eclipse 中的Phonegap)时遇到问题:

function FileStore(onsuccess, onfail){
    //chain of Phonegap File API handlers to get certain directories

    function onGetSupportDirectorySuccess(dir){
        //stuff
        onsuccess();
    }

    function getDirectory(dir){
        return "something" + dir; 
    }
}

var onFileStoreOpened = function(){
    if (window.file_store instanceof FileStore){
      console.log('window.file_store is a FileStore');
      console.log(window.file_store.getDirectory('something'));
    }
}

var onDeviceReady = function(){
  window.file_store = new FileStore(onFileStoreOpened, onFileStoreFailure);  
}

在这里,我想做一些事情来初始化应用程序的文件服务,然后在我的回调初始化中使用它们。我在 LogCat 中收到以下错误消息:

07-03 06:26:54.942: D/CordovaLog(223): file:///android_asset/www/index.html: Line 40 : window.file_store is a FileStore
07-03 06:26:55.053: D/CordovaLog(223): file:///android_asset/www/cordova-1.8.1.js: Line 254 : Error in success callback: File7 = TypeError: Result of expression 'window.file_store.getDirectory' [undefined] is not a function.

在移动代码并删除 getDirectory() 中的所有内容以确保它有效之后,我什至不确定我是否理解错误消息,这表明 getDirectory() 不被视为窗口的成员函数。 file_store,即使 window.file_store 被识别为 FileStore 对象。这对我来说毫无意义,所以我想这种解释是不正确的。任何启示将不胜感激。

从那以后,我尝试了以下方法:

window.file_store = {
    app_data_dir : null,
    Init: function(onsuccess, onfail){
        //chain of Phonegap File API handlers to get directories
        function onGetSupportDirectorySuccess(dir){
            window.file_store.app_data_dir = dir;
            console.log("opened dir " + dir.name);
            onsuccess();
        }
    },

    GetDirectory : function(){
        return window.file_store.app_data_dir; //simplified
    }
}


var onFileStoreOpened = function(){
    var docs = window.file_store.getDirectory();
    console.log('APPDATA: ' + docs.fullPath);
}

var onDeviceReady = function() {
    window.file_store.Init(onFileStoreOpened, onFileStoreFailure);  
}

我得到

D/CordovaLog(224): file:///android_asset/www/base/device.js: Line 81 : opened dir AppData
D/CordovaLog(224): file:///android_asset/www/cordova-1.8.1.js: Line 254 : Error in success callback: File7 = TypeError: Result of expression 'docs' [null] is not an object.

我在这里要做的就是确保在启动时存在某些目录(除了一个之外,我已经删除了所有目录),保存目录对象以供将来使用,然后在所有初始化完成后检索并使用它,我不想要全局命名空间中的所有内容。当然,我希望能够在必要时使用特定的实例,我很不安我不能让它这样工作,因为它表明我的理解存在问题,但我什至无法做到这一点与一个单一的、全球性的合作。这是 Javascript 问题还是 Phonegap 问题?

4

2 回答 2

1

As it stands, your getDirectory function is a private function within FileStore. If you wanted to make it a 'member' or 'property' of FileStore, you would need to alter it a little within FileStore to make it like this:

 this.getDirectory = function(dir){ };

or leave it how it is and then set a property....

this.getDirectory = getDirectory();

this way when new FileStore is called it will have getDirectory as a property because the 'this' keyword is always returned when calling a function with 'new'

Hope this quick answer helps. There's lots of stuff on the goog about constructor functions.

于 2012-07-03T07:26:47.470 回答
1

你理解正确。getDirectory 是一个私有函数,不能使用 file_store 实例调用。

在浏览器中试试这个。

function FileStore(onsuccess, onfail){
   function onGetSupportDirectorySuccess(dir){
        //stuff
        onsuccess();
   }

   this.getDirectory = function (dir){
   return "something" + dir; 
   }
}

window.file_store = new FileStore('', '');  //the empty strings are just placeholders.
if (window.file_store instanceof FileStore){
    console.log('window.file_store is a FileStore');
    console.log(window.file_store.getDirectory('something'));
}

这将证明基本的 js 代码工作正常。如果在 PhoneGap 中使用时仍有问题,请发表评论。

于 2012-07-03T08:16:02.423 回答