检查object是否是jQuery对象Check if object is a jQuery object

文章资讯 2020-07-19 11:32:22

检查object是否是jQuery对象Check if object is a jQuery object

example: 例:

var o = {};
var e = $('#element');

function doStuff(o) {
if (o.selector) {
console.log('object is jQuery');
}
}

doStuff(o);
doStuff(e);
obviously, the code above works but it's not safe. 显然,上面的代码有效,但不安全。 You could potentially add a selector key to the o object and get the same result. 您可以将选择键添加到o对象并获得相同的结果。 Is there a better way of making sure that the object actually is a jQuery object? 有没有更好的方法来确保对象实际上是一个jQuery对象?

Something in line with (typeof obj == 'jquery') 符合的东西(typeof obj == 'jquery')