antonpug
antonpug

Reputation: 14286

How is this argument making its way to a called function?

code:

function onDeviceReady() {
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}

function gotFS(fileSystem) {
    fileSystem.root.getFile("readme.txt", {create: true, exclusive: false}, gotFileEntry, fail);
}

What I don't understand is how does fileSystem get passed when there are no arguments passed with gotFS in the requestFileSystem?

Upvotes: 0

Views: 122

Answers (3)

Nathan Hughes
Nathan Hughes

Reputation: 96394

The call to requestFileSystem is receiving the function gotFS as a parameter. gotFS isn't being invoked, a reference to the function is getting passed in. If gotFS was being evaluated you would see parens after it. (Also, parameters are not verified in Javascript, so you can call a function with less or more arguments than expected.)

Upvotes: 1

Paul Sullivan
Paul Sullivan

Reputation: 2875

you have passed a function pointer to

window.requestFileSystem

in that method they can invoke and pass in any object they like

i.e. (psuedocode)

function window.requestFileSystem(localFs, someInt, functionDelegateToCallWithFS, fail)
{
 //blah
 var theFileSystemObject = fromSomwhereElse.get();
 functionDelegateToCallWithFS(theFileSystemObject);
 //blah
}

Upvotes: 0

gen_Eric
gen_Eric

Reputation: 227270

gotFS is passed as a variable (a callback). When requestFileSystem is ready to, it calls gotFS and passes the parameter.

Take this example:

function A(callback){
    callback('hello world');
}

function B(test){
    alert(test);
}

A(B);

A is passed B. A then calls B, passing 'hello world' to it.

Upvotes: 1

Related Questions