开发者

Jquery object caching

开发者 https://www.devze.com 2023-02-10 17:33 出处:网络
Is this the proper way to cache an object for later use. var x = $value.children().andSelf().filter(\'embed\'),

Is this the proper way to cache an object for later use.

var x = $value.children().andSelf().filter('embed'),

vsrc = x.attr开发者_Go百科('src'),
vwidth = x.attr('width'),
vheight = x.attr('height');


Yes.

However, you could improve your formatting:

var x = $value.children().andSelf().filter('embed'),
    vsrc = x.attr('src'),
    vwidth = x.width(),
    vheight = x.height();

Also, you can use width() and height() to retrieve the dimensions of the element.


Yes, this is exactly how to "reuse" a jQuery object if you don't want to chain it. (Chaining can make your code hard to read if you overuse it, so this is often a good alternative.)

It's common, though not at all a standard, to give your variable a name starting with $ to mark it as a jQuery object:

var $embed = $value.children().andSelf().filter('embed');

(Joel on "apps hungarian" notation.)

0

精彩评论

暂无评论...
验证码 换一张
取 消