2012-04-23 22 views
0

代码:这个参数是如何进入被调用函数的?

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

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

我不明白的是如何文件系统获得通过时,有没有用在requestFileSystem gotFS传递的参数?

回答

1

对requestFileSystem的调用正在接收函数gotFS作为参数。 gotFS没有被调用,对函数的引用正在被传入。如果正在评估gotFS,你会在它后面看到parens。 (另外,参数未在Javascript中验证,因此您可以使用比预期更少或更多的参数调用函数。)

1

gotFS作为变量(回调)传递。当requestFileSystem准备好时,它会调用gotFS并传递参数。

拿这个例子:

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

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

A(B); 

A传递BA然后调用B,将'hello world'传递给它。

0

你在那个方法传递的函数指针

window.requestFileSystem 

他们可以调用,并通过他们喜欢的任何对象

即(伪代码)

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

TA火箭我编辑代码相同 – 2012-04-23 19:17:44

+0

我是一个编辑忍者:-P – 2012-04-23 19:18:51

相关问题