Javascript 将变量传递给回调函数

Javascript 将变量传递给回调函数,javascript,cordova,callback,Javascript,Cordova,Callback,如何获取FileDoesNodeList回调的变量、url和名称: window.checkIfFileExists = function(path, url, name) { return window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, (function(fileSystem) { return fileSystem.root.getFile(path, { create: false }, fi

如何获取FileDoesNodeList回调的变量、url和名称:

window.checkIfFileExists = function(path, url, name) {
  return window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, (function(fileSystem) {
    return fileSystem.root.getFile(path, {
      create: false
    }, fileExists, fileDoesNotExist);
  }), getFSFail);
};

fileDoesNotExist = (fileEntry, url, name) ->
  downloadImage(url, name)
window.checkIfFileExists = function(path, url, name) {
  return window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, (function(fileSystem) {
    return fileSystem.root.getFile(path, {
      create: false
    }, fileExists, function(){
        //manually call and pass parameters
        fileDoesNotExist.call(this,path,url,name);
    });
  }), getFSFail);
};

您可以传入一个匿名函数,并将它们添加到回调调用中:

window.checkIfFileExists = function(path, url, name) {
  return window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, (function(fileSystem) {
    return fileSystem.root.getFile(path, {
      create: false
    }, fileExists, fileDoesNotExist);
  }), getFSFail);
};

fileDoesNotExist = (fileEntry, url, name) ->
  downloadImage(url, name)
window.checkIfFileExists = function(path, url, name) {
  return window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, (function(fileSystem) {
    return fileSystem.root.getFile(path, {
      create: false
    }, fileExists, function(){
        //manually call and pass parameters
        fileDoesNotExist.call(this,path,url,name);
    });
  }), getFSFail);
};

phoneGap的
getFile
函数有两个回调函数。这里使用
filedoesnotextist
时犯的错误是,它应该调用两个函数,而不是引用一个变量

类似于以下内容的操作将起作用:

window.checkIfFileExists = function(path, url, name) {
  return window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, (function(fileSystem) {
    return fileSystem.root.getFile(path, {
      create: false
    }, 
    function(e) {
      //this will be called in case of success
    },
    function(e) {
      //this will be called in case of failure
      //you can access path, url, name in here
    });
  }), getFSFail);
};