`
iwebcode
  • 浏览: 2007651 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

jQuery源码分析6: jQuery.isEmptyObject与jQuery.isPlainObject

 
阅读更多
jQuery源码分析6: jQuery.isEmptyObject与jQuery.isPlainObject

var hasOwn = Object.prototype.hasOwnProperty,

isEmptyObject: function( obj ) {
for ( var name in obj ) {
return false;
}
return true;
},

isPlainObject: function( obj ) {
// 必须是一个Object,同时需要过滤掉DOM对象
// Because of IE, we also have to check the presence of the constructor property.
if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
return false;
}

try {
// 拥有自己的constructor属性必然不是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;
}

// Own properties are enumerated firstly, so to speed up,
// if last one is own, then all properties are own.

var key;
for ( key in obj ) {}

return key === undefined || hasOwn.call( obj, key );
},

测试:
jQuery.isPlainObject({}); // true

分享到:
评论

相关推荐

    jQuery 1.4.1 中文参考

    11.4.5 jQuery.isPlainObject(obj) 193 11.5 字符串操作 194 11.5.1 jQuery.trim(str) 194 11.6 URL 194 11.6.1 jQuery.param(obj, [traditional]) 194 11.7 插件编写 196 11.7.1 jQuery.error(message) 196 12. ...

    jQuery 1.5 API 中文版

    $.jQuery( selector [, context] ), .jQuery( element ), .jQuery( elementArray ), .jQuery( jQueryObject ), .jQuery( ) $.jQuery( html [, ownerDocument] ), .jQuery( html, props ) $.jQuery( fn ) jQuery ...

    JQuery权威指南源代码

    第1章 第一个简单的jQuery程序 jQuery事件的链式写法 控制jQuery对象 jQuery控制CSS样式 第2章 使用JavaScript实现隔行变色 ...DOM对象与jQuery对象的类型转换 第11章 应用案例-聊天室系统 应用案例_图片切割

    jQuery权威指南-源代码

    其次详细讲解了jQuery的各种选择器、jQuery操作DOM的方法、jQuery中的事件与应用、jQuery中的动画和特效、Ajax在jQuery中的应用,以及各种常用的jQuery插件的使用方法和技巧,所有这些知识点都配有完整的示例(包括...

    jquery1.11.0手册

    jQuery 1.11.0 速查表 核心 jQuery 核心函数 jQuery([sel,[context]]) jQuery(html,[ownerDoc])1.8* jQuery(callback) jQuery.holdReady(hold) jQuery 对象访问 each(callback) size() length selector ...

    jQuery1.4 API

    jQuery 1.4.1 速查表 -- Shawphy, 原作:G. Scott Olson 核心 jQuery 核心函数 ...$.isEmptyObject(obj) $.isPlainObject(obj) 字符串操作 $.trim(str) URL $.param(obj, [traditional]) 插件编写 $.error(message)

Global site tag (gtag.js) - Google Analytics