Joh*_*ohn 2 javascript callback
在这个例子中,第二个函数是第二个:
<script>
function first (callback){
alert ("I am first");
callback();
}
function second (){
alert ("I am second");
}
first(second);
</script>
Run Code Online (Sandbox Code Playgroud)
但在此示例中,首先处理第二个函数.为什么在调用第二个之后添加()会产生影响....
<script>
function first (callback){
alert ("I am first");
callback();
}
function second (){
alert ("I am second");
}
first(second());
</script>
Run Code Online (Sandbox Code Playgroud)
置于()保持函数的变量之后将调用该函数.
first(second);调用first并将second函数作为参数传递.first然后将调用callback哪个是相同的second
first(second());调用second,然后调用first并传递返回值second()作为参数.first然后将调用undefined(和错误),因为这是返回值second().