javascript - jQuery.each 是否在每次迭代中计算集合表达式?

标签 javascript jquery

我有这样的代码:

$.each($.parseJSON(data).content,function(){});

我想知道 $.parseJSON(data).content 是否会在每次迭代时评估,还是只评估一次?这将显着影响性能。

最佳答案

函数调用的参数始终在调用函数之前求值。这不是 jQuery 特有的。请参阅the specification :

3 . Let argList be the result of evaluating Arguments, producing an internal list of argument values (see 11.2.4).

...

8 . Return the result of calling the [[Call]] internal method on func, providing thisValue as the this value and providing the list argList as the argument values.

<小时/>

为了清楚起见,for 中的情况不是循环:

for(var i = 0; i < $.parseJSON(data).content.length; i++) {

}

这里,每次都会计算第二个表达式。这是有道理的,因为在一个简单的循环中,您想要重新评估条件 i < length在每次迭代时。但您不想想要运行$.parseJSON在每次迭代时。所以在 for循环,分解表达式确实有意义。

关于javascript - jQuery.each 是否在每次迭代中计算集合表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14693469/

相关文章:

javascript - JS 问题通过在 2 个不同的 div 框中显示获取的 MySQL 数据 [PHP, MySQL & JS]

相当于 Ruby 的 `send` 的 Javascript

javascript - 如何确保 $.each 推送 jQuery 中的所有延迟?

javascript - Jquery 从外部链接扩展

javascript - 如何在 KendoUI 中获取选定的行及其数据项?

javascript - 简单的嵌套 setTimeout() 只运行一次 (JavaScript)

javascript - html angularjs中的if语句

asp.net - 完成方法后面的代码后执行javascript方法?

javascript - 当 JQuery 按钮获得焦点并按下空格键时,如何防止页面滚动

jquery - 为什么我的 jQuery toggleClass 不工作?