jsaye
jsaye

Reputation: 914

Passing extra parameters to a callable object in Javascript

I know that in Javascript you can add extra parameters to a function. For example;

function sum(){

var result = 0;
for(var i = 0;i<arguments.length;i++){
  result += arguments[i];
}
return result;
}

And then call sum(1,2,3,4). Right now I'm using Phonegap and I would like pass extra parameters to a callable object. (Doing something similar to what I explained before.)

In Phonegap you can access the filesystem by doing this:

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, success, fail);
function success(fileSystem){
//Do something
 }

Is it possible to do something similar to this?

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, success(option1, option2), fail);
    function success(fileSystem){
    //Do something
      if(option1 > option2){
       //Do something even interesting
       }

     }

Since I'm new to both, Javascript and Phonegap I'm not sure if it is possible to do something like this. I would like to avoid using global variables.

Upvotes: 1

Views: 1243

Answers (5)

Jamiec
Jamiec

Reputation: 136124

Perhaps you could do something like encapsulate it all in a function

function doFileSystemStuff(option1,option2){
   window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, success, fail);
   function success(fileSystem){
      //Do something
      if(option1 > option2){
       //Do something even interesting
      }
    }
}

Then call it approproiately with option1 and option2

Upvotes: 1

zerkms
zerkms

Reputation: 254944

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) { return success(fileSystem, option1, option2); }, fail);
    function success(fileSystem, option1, option2){
    //Do something
      if(option1 > option2){
       //Do something even interesting
       }

     }

m?

Upvotes: 5

closure
closure

Reputation: 7452

Do it like this:

function getFileSystem() {

    //set your option1, option2
    var option1 = 1, option2 = 2;

    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {

      //Do something

      function innerSuccess(fileSystem, option1, option2) {
       //Do something even interesting

      }
    }, fail);

}

Upvotes: 0

Tim S.
Tim S.

Reputation: 13843

Most likely not.

Your success-handler is called in the function requestFileSystem. My guesses are that the function doesn't include any other parameters.

Since you don't want to use global vars, there's a solution in the middle: put everything in a function:

(function() {
    var option1 = 0, option2 = 1;

    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, success, fail);

    function success(filesystem) {
        console.log(option1, option2); // will display 0 and 1
    }
})(); // calls itself

console.log(option1, option2); // undefined

That way you can still set option1 and `option2, access them in your methods, without making them global!

Upvotes: 0

lavrik
lavrik

Reputation: 1474

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
function(option1, option2){ success(option1, option2)}, fail);
function success(fileSystem){
//Do something
  if(option1>option1){
   //Do something even interesting
   }

 }

Upvotes: 0

Related Questions