最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
jquery的isPlainObject检查是不是对象例子
时间:2022-06-25 17:47:33 编辑:袖梨 来源:一聚教程网
对象一词经常在日常生活中使用,在js中对象{}也是很常用,于是得有个“检查是不是对象”的方法吧,然后jquery提供isPlainObject方法。。。
//jquery1.11.2
isPlainObject: function( obj ) {
var key;
// Must be an Object.
// Because of IE, we also have to check the presence of the constructor property.
// Make sure that DOM nodes and window objects don't pass through, as well
if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
return false;
}
try {
// Not own constructor property must be Object
if ( obj.constructor &&
!hasOwn.call(obj, "constructor") &&
!hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
return false;
}
} catch ( e ) {
// IE8,9 Will throw exceptions on certain host objects #9897
return false;
}
// Support: IE<9
// Handle iteration over inherited properties before own properties.
if ( support.ownLast ) {
for ( key in obj ) {
return hasOwn.call( obj, key );
}
}
// Own properties are enumerated firstly, so to speed up,
// if last one is own, then all properties are own.
for ( key in obj ) {}
return key === undefined || hasOwn.call( obj, key );
},
测试用例是:
asyncTest("isPlainObject", function() {
expect(15);
var pass, iframe, doc,
fn = function() {};
// The use case that we want to match
ok( jQuery.isPlainObject({}), "{}" );
// Not objects shouldn't be matched
ok( !jQuery.isPlainObject(""), "string" );
ok( !jQuery.isPlainObject(0) && !jQuery.isPlainObject(1), "number" );
ok( !jQuery.isPlainObject(true) && !jQuery.isPlainObject(false), "boolean" );
ok( !jQuery.isPlainObject(null), "null" );
ok( !jQuery.isPlainObject(undefined), "undefined" );
// Arrays shouldn't be matched
ok( !jQuery.isPlainObject([]), "array" );
// Instantiated objects shouldn't be matched
ok( !jQuery.isPlainObject(new Date()), "new Date" );
// Functions shouldn't be matched
ok( !jQuery.isPlainObject(fn), "fn" );
// Again, instantiated objects shouldn't be matched
ok( !jQuery.isPlainObject(new fn()), "new fn (no methods)" );
// Makes the function a little more realistic
// (and harder to detect, incidentally)
fn.prototype["someMethod"] = function(){};
// Again, instantiated objects shouldn't be matched
ok( !jQuery.isPlainObject(new fn()), "new fn" );
// DOM Element
ok( !jQuery.isPlainObject( document.createElement("div") ), "DOM Element" );
// Window
ok( !jQuery.isPlainObject( window ), "window" );
pass = false;
try {
jQuery.isPlainObject( window.location );
pass = true;
} catch ( e ) {}
ok( pass, "Does not throw exceptions on host objects" );
// Objects from other windows should be matched
Globals.register("iframeDone");
window.iframeDone = function( otherObject, detail ) {
window.iframeDone = undefined;
iframe.parentNode.removeChild( iframe );
ok( jQuery.isPlainObject(new otherObject()), "new otherObject" + ( detail ? " - " + detail : "" ) );
start();
};
try {
iframe = jQuery("#qunit-fixture")[0].appendChild( document.createElement("iframe") );
doc = iframe.contentDocument || iframe.contentWindow.document;
doc.open();
doc.write("
doc.close();
} catch(e) {
window.iframeDone( Object, "iframes not supported" );
}
});
以上判断的有dom,window,{},function,null,undefined,new
Object,Number,String,Boolean,Array,Date等,但那么问题来了:
var obj = {};//这是一个对象吧?
console.log($.isPlainObject(obj));//true
var obj2 = {"xuexb": 123};//这也是一个对象吧?
console.log($.isPlainObject(obj2));//true
var obj3 = {"nodeType": "啦啦啦"};//这也是一个对象吧?
console.log($.isPlainObject(obj3));//false
怎么回事呢? 其实原因就在if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {这一判断,jquery认为只要对象的key里有nodeType就是dom,那么俺的对象就不能叫这个了?当然jquery这么做肯定有她的原因,只是俺不知道罢了
相关文章
- 无限暖暖搭乘大鸟巴士怎么玩 公测第三天每日任务做法介绍 12-27
- 奇迹暖暖绚光护佑搭配攻略 奇迹暖暖绚光护佑过关攻略 12-27
- 绝区零月城柳意像影画怎么样 12-27
- 无限暖暖翩翩愿飞去怎么样 12-27
- 黑神话悟空1.0.12.16581版本更新公告 12-27
- 光遇12.27红石碎片在哪里 12-27