我有这样的代码:
$.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
, providingthisValue
as the this value and providing the listargList
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/