匿名回调函数说明

djf*_*939 4 javascript function callback node.js

我正在刷回回调函数,并从http://javascriptissexy.com/understand-javascript-callback-functions-and-use-them/#看到以下段落

"当我们将回调函数作为参数传递给另一个函数时,我们只传递函数定义.我们不是在参数中执行函数.换句话说,我们没有传递函数与尾对执行括号()就像我们执行函数时一样.

并且由于contains函数在其参数中具有回调函数作为函数定义,因此它可以随时执行回调."

有人可以解释一下吗?以下是他们提供的两个例子.

?//The item is a callback function
$("#btn_1").click(function() {
  alert("Btn 1 Clicked");
});
Run Code Online (Sandbox Code Playgroud)

这是另一个例子:

var friends = ["Mike", "Stacy", "Andy", "Rick"];
?
friends.forEach(function (eachName, index){
console.log(index + 1 + ". " + eachName); // 1. Mike, 2. Stacy, 3. Andy, 4. Rick?
});
Run Code Online (Sandbox Code Playgroud)

"请注意,回调函数不会立即执行.它在包含函数体内的某个指定点处被"回调"(因此得名).所以,即使第一个jQuery示例如下所示:

//The anonymous function is not being executed there in the parameter. ?
?//The item is a callback function
   $("#btn_1").click(function() {
     alert("Btn 1 Clicked");
   });
Run Code Online (Sandbox Code Playgroud)

稍后将在函数体内调用匿名函数.即使没有名称,它仍然可以通过包含函数的arguments对象来访问."

对于第一个使用jquery的例子,他们究竟在说什么.如果单击#btn_1元素,是否会执行匿名函数?我假设如果单击按钮将执行它,但该段落的措辞令人困惑?

同样,对于第二个例子,他们不需要调用他们作为参数传递的函数吗?

小智 9

在这两个示例中,您都将匿名函数作为参数传递.

$("#btn_1").click(function() {
  alert("Btn 1 Clicked");
});
Run Code Online (Sandbox Code Playgroud)

jQuery的click方法将函数作为其第一个参数.所以想象一下click的函数定义是这样的:

function click(fn) {
    // fn will contain a reference to any 
    // function passed as the first parameter to click
    // merely calling fn does nothing, because you are just 'calling' 
    // the reference.
    fn;
    // Since what is inside of fn is a function, you can execute it 
    // with the () syntax
    fn();
}
// Now, you have many ways to pass a function as the first parameter to the function

// 1. As an anonymous function:
click(function() {
    console.log("Hi");
});

// 2. As a named function:
click(function hello() {
    console.log("Hi");
});

// 3. As a reference to a function declaration
function hiThere() {
    console.log("Hi");
}
click(hiThere);

// 4. As a variable that holds an anonymous function inside
var howdy = function () {
    console.log("howdy");
};
click(howdy);
Run Code Online (Sandbox Code Playgroud)

试想一下,函数就像变量,但它们内部的内容可以()在最后执行.

function hi() {
    console.log('bye');
}

hi; // Calls the reference, but does not execute it. This does nothing.
hi.toString(); // Returns the function as a string
hi(); // Executes the code within the function
Run Code Online (Sandbox Code Playgroud)

每当你声明一个命名函数时,你可以根据它的名字来处理它,就像你对变量一样.当然,与变量不同,它们包含可执行代码,而不是值.

你不能引用匿名函数,因为它很好......匿名.除非你把它放在一个有名字的东西里面,比如说var.

var iHoldAFunctionInside = function () {
    console.log('Im not so anonymous now');
};
iHoldAFunctionInside(); // Logs "Im not so anonymous now"
Run Code Online (Sandbox Code Playgroud)

这就是为什么你可以将匿名函数作为参数传递给函数,并且可以将它作为回调执行.因为参数现在"保存"其中的匿名函数:

function iExecuteYourCallback(callback) {
    // callback contains the anonymous function passed to it
    // Similar to doing:
    // var callback = function () { };
    callback();
}
iExecuteYourCallback(function() {
    console.log('Im a callback function!');
});
Run Code Online (Sandbox Code Playgroud)

希望这有助于清除一些事情.

  • 这是一个非常好的总结(我不能再显示了). (2认同)
  • @ djfkdjfkd39939:它基本上就像你将函数声明为`function foo(){console.log('foo'); }`,但从不调用`foo()`.该函数存在一段时间,但它没有做任何事情. (2认同)