com*_*dos 12 performance jquery chaining
这些在速度方面是否相同?
$(this).attr("date",date);
$(this).attr("date_start",date_start);
$(this).attr("heure_start",heure_start);
Run Code Online (Sandbox Code Playgroud)
要么
$(this).attr("date",date).attr("date_start",date_start).attr("heure_start",heure_start);
Run Code Online (Sandbox Code Playgroud)
即使第二个更快,最好将它单独编写以使代码更具可读性?
Den*_*ret 28
不,这两者在速度上并不相同.
$(this)
每次都构建一个新的jQuery对象.根据具体情况this
,这可能是一项复杂的操作.
所以第二种形式更快.
请注意,为了便于阅读,您可以将其编写为
$(this)
.attr("date",date)
.attr("date_start",date_start)
.attr("heure_start",heure_start);
Run Code Online (Sandbox Code Playgroud)
如果由于中间有其他代码行而无法链接操作,则还可以缓存该对象.这通常是:
var $this = $(this);
$this.attr("date", date);
$this.attr("date_start", date_start);
$this.attr("heure_start", heure_start);
Run Code Online (Sandbox Code Playgroud)
还要注意attr可以将地图作为参数:
$(this).attr({
date: date,
date_start: date_start,
heure_start: heure_start
});
Run Code Online (Sandbox Code Playgroud)
出于可读性目的,您可以将行拆分为
$(this)
.attr("date",date)
.attr("date_start",date_start)
.attr("heure_start",heure_start);
Run Code Online (Sandbox Code Playgroud)
我知道这应该是一个评论,但是间距本来就没有意义.