开发者

What to do with $(undefined)?

I accidentally came across $(undefined) in a statement below, where params is an object:

var $this = $(params._this) || $(this);

This did not wo开发者_如何学JAVArk, as $(params._this) is a jQuery object and is always evaluated to true.

Funny enough, I was not sure how to check for it. It is not an empty object (i.e. $.isEmptyObject($(undefined)) == false), nor is it a function or a "plain object" (i.e. $.isPlainObject()).

I ended up modifying the statement to the following:

var $this = (params._this == undefined) ? $(params._this) : $(this);

My question is, is there any way to "evaluate" (not sure what word to use) $(undefined)? Is there any use for this?


var $this = $(params._this || this);


$(undefined) is just an empty jQuery object; it's equivalent to $(). Sometimes it's used instead of $(document) in the common idiom $(document).ready(...).


The most suitable way in your case seems to be what DanC said, but you can check for an undefined jquery object by checking it's selector :

$(undefined).selector == ''

In your case :

var $this = $(params._this).selector != '' ? $(params._this) : $(this);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜