判断一个对象是否为jquery对象的方法

当我们在用jquery的each做循环遍历的时候常常会使用到this,而有时候我们不知道this所指的到底是什么,因为要使用jquery的方法前提此对象必须是jquery对象。
另外要判断一个javascript的对象是什么类型,可以使用typeof,但是typeof只能判断出js的基础对象(string,boolean,number,object).
判断一个对象是否为jquery对象可以用 obj instanceof jQuery

var obj = $("div");
if (obj instanceof jQuery) {
	alert("这是一个jQuery对象");
} else {
	alert("这是一个其它对象")
}

obj.each(function() {
	console.log(this instanceof jQuery); //false
	console.log($(this) instanceof jQuery); //true
})

其他:

$(this)[0]
$(this).get(0)
$(this).toArray()[0];
以上3种写法等价

不是转化,是获取内部的DOM对象引用.
jQuery的有2个核心 一个是查询 一个是方法插件
$('..')是一个工厂方法 内部调用了查询(根据css的定位方式获取符合条件的所有dom引用,还有其他特殊情况情况)
并返回一个 jQuery对象 该对象是一个扩展的Array 所有查询的dom全放在这个Array中.
则 get(index) 内部 代码是  return this[index]

原文地址:https://www.cnblogs.com/bigdesign/p/4554022.html