开发者

Jquery object caching

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜