- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在复习回调函数并从 http://javascriptissexy.com/understand-javascript-callback-functions-and-use-them/# 中看到以下段落
“当我们将回调函数作为参数传递给另一个函数时,我们只是传递了函数定义。我们没有执行参数中的函数。换句话说,我们没有传递带有尾随对的函数就像我们执行函数时一样执行括号 ()。
并且由于包含函数在其参数中具有回调函数作为函数定义,因此它可以随时执行回调。"
谁能解释一下?这是他们提供的两个示例。
//The item is a callback function
$("#btn_1").click(function() {
alert("Btn 1 Clicked");
});
这是另一个例子:
var friends = ["Mike", "Stacy", "Andy", "Rick"];
friends.forEach(function (eachName, index){
console.log(index + 1 + ". " + eachName); // 1. Mike, 2. Stacy, 3. Andy, 4. Rick
});
“请注意,回调函数不会立即执行。它会在包含函数主体内的某个指定点“回调”(因此得名)。因此,即使第一个 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");
});
稍后将在函数体内调用匿名函数。即使没有名称,以后仍然可以通过包含函数的参数对象访问它。”
对于 jquery 的第一个例子,他们到底在说什么。如果点击#btn_1 元素,是否会执行匿名函数?我假设它会在单击按钮时执行,但这段文字中的措辞令人困惑?
同样,对于第二个示例,他们是否不需要调用作为参数传递的匿名函数?
最佳答案
在这两个示例中,您都将匿名函数作为参数传递。
$("#btn_1").click(function() {
alert("Btn 1 Clicked");
});
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);
想象一下,函数就像变量,但它们内部有内容,可以在最后用 ()
执行。
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
每当你声明一个命名函数时,你都可以根据它的名字来处理它,就像你处理变量一样。当然,与变量不同的是,它们在内部保存可执行代码,而不是值。
您不能引用匿名函数,因为它是……匿名的。 UNLESS,您将它保存在有名称的东西中,例如 var
。
var iHoldAFunctionInside = function () {
console.log('Im not so anonymous now');
};
iHoldAFunctionInside(); // Logs "Im not so anonymous now"
这就是为什么您可以将匿名函数作为参数传递给函数,并且它可以将其作为回调执行。因为参数现在“保存”了其中的匿名函数:
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!');
});
希望这有助于澄清一些事情。
关于javascript - 匿名回调函数说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29933522/
我正在查看预先重写的 jQuery 代码。我无法理解以下代码。 $('body > *:not(#print-modal):not(script)').clone(); 最佳答案 此选择器匹配以下任何
所以我开始学习MySQL,我对表有点困惑,所以我想澄清一下。数据库中可以有多个表吗?例如: Database1 -Table1 -Username -Password -Table2 -Name
我在 PostgreSQL 中编写了一个函数,其代码如下: for (i = 0; i str[0][i]); values[i] = datumCopy(dat_value,
oid: 行的对象标识符(对象 ID)。这个字段只有在创建表的时候使用了 WITH OIDS ,或者是设置了default_with_oids 配置参数时出现。 这个字段的类型是 oid (和字段同
我在搜索最大连接设备数时发现了 a post大致说: 当使用 P2P_STAR 时,最大设备数量为 10,因为此 topoly 使用 Wi-Fi 热点。也就是说,如果您没有路由器。 这让我问了两个问题
我不明白为什么会这样: Printf.sprintf "%08s" "s" = Printf.sprintf "%8s" "s" - : bool = true 换句话说,我希望: Printf.sp
我正在遵循 Grails in Action 中的示例。我有一个问题,如何理解 addTo*()功能有效。 我有一个简单的域:具有以下关系的用户、帖子、标签: 用户1对M发帖 用户一对一标签 发布 M
请问为什么行 "b[0]= new Child2();"在运行时而不是在编译时失败。请不要检查语法,我只是在这里做了 class Base {} class Child1 : Base {} clas
所以我想进一步加深我对套接字的理解,但是我想首先从最低级别开始(在C语言中,而不是在汇编中大声笑) 但是,我处理的大多数站点都使用SOCK_STREAM或SOCK_DGRAM。但是我已经阅读了Beej
好吧,我对 javascript 语法了解甚少,而且我对 null 的行为感到非常困惑。关于空值有很多讨论,但我似乎无法找出问题所在!请帮我。这是脚本。 var jsonData = '';
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭5 年前。 Improve thi
问题: SeriesSum 类旨在计算以下系列的总和: 类名:SeriesSum 数据成员/实例变量: x:存储整数 n:存储术语数量 sum:用于存储系列总和的双变量 成员函数: SeriesSum
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 9 年前。 Improve this ques
今天我在 logcat 中注意到以下内容: D/OpenGLRenderer:0xa2c70600 (CardView) 上的 endAllStagingAnimators,句柄为 0xa2c9d35
如何创建值有序对的列表,例如list1 [(x, y), (x1, y1) ...].?? 学习如何创建此列表后,我需要知道如何将 x 值提供给列表中的用户输入并搜索 x 的下一个值并显示有序对 (x
我在存储过程中有以下逻辑。 这里完成了什么? 如果color为null,替换为'' IF ISNULL(@color, '') <> '' BEGIN END 最佳答案 它等同于: IF (@colo
我知道.Net中的接口(interface)定义了接口(interface)和继承它的类之间的契约。刚刚完成了一个大量使用数据访问层接口(interface)的项目,这让我开始思考。 . .有什么大不
如何防止基类方法被子类覆盖 最佳答案 您不需要做任何特别的事情:默认情况下方法是不可覆盖的。相反,如果您希望该方法可重写,则必须将 virtual 关键字添加到其声明中。 但是请注意,即使方法不可重写
我已阅读以下有关工厂模式的文章 here 请仅引用Class Registration - avoiding reflection这一部分。 这个版本在没有反射的情况下实现了工厂和具体产品之间的减少耦
我正在学习 Java 类(class),但无法完全理解下一课的内容。 目的:本课的目的是通过创建一个模拟 for-each 循环如何工作的替代方案来解释 for-each 循环的工作方式。 在上一课中
我是一名优秀的程序员,十分优秀!