基本Node.jscallback分配给variables

一般来说我一直在callback中挣扎。 我试图回到基本的概念来理解,这是我迄今为止所了解的(是的,基本的)。

function RandHash (callback) { ds_hash = Math.floor((Math.random()*10000)+1); callback(); } function CompiledHash (){ console.log(ds_hash); } var ds_hash; RandHash(Compiled Hash); 

将产生随机数。

但是,我迷失了如何获得callback函数返回的“ds_hash”variables。

似乎这将工作:

 var ds_hash; ds_hash = RandHash(Compiled Hash); 

它不。 如果我尝试返回值如下所示:

 function CompiledHash (){ return ds_hash; } 

它什么都不做。

请帮我解决一下这个。 似乎我花了90%的时间用callbackdebugging节点。 我已经构build了一些体面的应用程序,但是由于我有这个心理障碍,所有事情都是通过asynchronous库来处理的。

谢谢。

你所做的第一个错误是RandHash没有返回任何东西。 函数中没有return语句,所以var anything = RandHash(alsoAnything)总是会导致anything未定义的内容。

函数是第一类variables

函数可以像variables一样使用,并作为parameter passing给函数。 这是一个JavaScript的强大function,你需要使用callback。 把一个函数想象成一个定义好的动作,然后你只是传递这个动作。

此外,应该使用callback来处理完成一个过程。 所以这个想法是,你知道在进程A之后会发生什么,以及它是什么意思产生的,所以你可以给进程A一个callback,一旦A终止就会被调用。 现在这里的情况不适合callback的情况。 这样做会容易得多

 function RandHash () { return Math.floor((Math.random()*10000)+1); } 

然后通过调用这个函数来获得你的散列,就像这样…

 var hash = RandHash(); 

您还需要了解javascriptsvariables范围,因为在RandHash函数中引用ds_hash时缺lessvar关键字,这意味着该分配缺省为全局范围。 这可能是造成您困惑的原因,因为您在ds_hash中的RandHash分配将是全局的,因此可以在CompiledHash ,这意味着某些函数仍然能够访问ds_hash值,尽pipe这不是正确的或正确的方法这个。

假设您最终将需要asynchronous处理

 function randHash (callback) { var hash = /* do hash calculation */ callback(hash); } function compileHash (hash) { /* do something using the hash variable passed to this function */ } randHash(compileHash); // pass the compileHash function as the callback 

你应该把你的variables作为parameter passing给callback。 这将有希望处理您的范围问题。

此外,小注,JavaScript中的函数通常应该是小写,如果他们不会用于new语句(即,一个JavaScript类)。

为了真正理解asynchronous行为,你应该尝试一些实际上是asynchronous的东西,并且传递值而不是全局variables,因为当你转向asynchronous函数时,这将永远不会工作:

 function RandHash (callback) { setTimeout(function() { var ds_hash = Math.floor((Math.random()*10000)+1); callback(ds_hash); // execute callback when async operation is done }, 300); } function CompiledHash(hash){ // pass hash console.log(hash); } RandHash(function(returned_hash) { // this is the callback function CompiledHash(returned_hash); // pass along the returned data }); 

一旦asynchronous函数(本例中为setTimeout)完成,并将数据作为parameter passing回来,就会执行callback函数。
callback参数只是作为parameter passing的函数。

 // regular function function doStuff(argument) { // where argument can be anything, also a function } // you can pass a string doStuff('string'); // or a function doStuff(function(argument_returned) { }); // and passing a function you can execute that function with arguments in the original function function doStuff(argument) { var argument_returned = 'string'; argument(argument_returned); // executes the passsed function }); 

一对夫妇笔记:

  1. 如果你想传播callback的结果,那么你需要在RandHash return它的输出

  2. ds_hash ,如果您打算将其返回,则不需要ds_hash成为全局variables

  3. RandHash(Compiled Hash); 是一个语法错误(注意variables名称中的空格)

尝试这个:

 function RandHash(callback) { var ds_hash = Math.floor((Math.random() * 10000) + 1); return callback(ds_hash); } function CompiledHash(ds_hash) { console.log(ds_hash); return ds_hash; } RandHash(CompiledHash); 

注意,没有全局variables。 callback函数返回一个值,执行callback的函数会传递它。

在样式注释中,您只应该大写您打算用作构造函数的函数的名称。

尝试这个:

 function RandHash (callback) { ds_hash = Math.floor((Math.random()*10000)+1); callback(ds_hash); } function CompiledHash(ds_hash){ console.log(ds_hash); } var ds_hash; RandHash(CompiledHash); 

对于callback和函数,理解variables作用域有点困难,你所做的不是真正的build议。 你可以传递参数到callback中,并且应该通过,但我会build议像这样构build它:

 function RandHash (callback) { var ds_hash = Math.floor((Math.random()*10000)+1); callback(ds_hash); } function CompiledHash(ds_hash){ console.log(ds_hash); } RandHash(CompiledHash); 

这会做

 function RandHash (callback) { var ds_hash = Math.floor((Math.random()*10000)+1); callback(ds_hash); } var CompiledHash = function (ds_hash){ console.log(ds_hash); } RandHash(CompiledHash);