【发布时间】:2012-03-06 23:06:26
【问题描述】:
Does creating and reusing a reference increase performance appreciably when the selector is $(this)?
当我在同一个范围内多次使用同一个选择器时,我会为我的 jQuery 选择器创建引用。下面的效率更高
var jSel = $('div.some_class input.some_other_class');
some_function1(jSel);
some_function2(jSel);
比这个
some_function1($('div.some_class input.some_other_class'));
some_function2($('div.some_class input.some_other_class'));
但是如果选择器只是$(this),其中this 是jQuery 方法中的一个dom 元素怎么办。我应该费心创建对$(this) 的引用并重复使用该引用,还是可以创建多个$(this) 选择器并期望获得相似的性能?
是以下
var jSel = $(this);
some_function1(jSel);
some_function2(jSel);
明显快于以下?
some_function1($(this));
some_function2($(this));
【问题讨论】:
标签: javascript jquery