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.)
精彩评论