作者:zccst
本次学习$.isPlainObject,是不是一个普通对象。测试对象是否是纯粹的对象(通过 "{}" 或者 "new Object" 创建的)
1,使用场景:
var o = {};
console.log($.isPlainObject(o));//如果是空对象就返回TRUE,否则返回FALSE。
2,下面看一下源码实现:
isPlainObject: function( obj ) {
// Not plain objects:以下三种情况不是普通对象(1)使用typeof判断;(2)DOM节点(3)window
// - Any object or value whose internal [[Class]] property is not "[object Object]"
// - DOM nodes
// - window
if ( jQuery.type( obj ) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
return false;
}
// Support: Firefox <20
// The try/catch suppresses exceptions thrown when attempting to access
// the "constructor" property of certain host objects, ie. |window.location|
// https://bugzilla.mozilla.org/show_bug.cgi?id=814622
try {
//obj的构造函数为真,且obj构造函数的原型对象里没有isPrototypeOf属性,则也不是纯粹的对象
if ( obj.constructor && !core_hasOwn.call( obj.constructor.prototype, "isPrototypeOf" ) ) {
return false;
}
} catch ( e ) {
return false;
}
// If the function hasn't returned already, we're confident that
// |obj| is a plain object, created by {} or constructed with new Object
return true;
}
判断过程:
var class2type = {};
var core_hasOwn = class2type.hasOwnProperty;
// Populate the class2type map
jQuery.each("Boolean Number String Function Array Date RegExp Object Error".split(" "), function(i, name) {
class2type[ "[object " + name + "]" ] = name.toLowerCase();
});
core_hasOwn.call( o.constructor.prototype, "isPrototypeOf" )与
o.constructor.prototype.hasOwnProperty( "isPrototypeOf" )是相同的
hasOwnProperty语法:obj.hasOwnProperty(prop) //返回值就是TRUE或FALSE
如果您觉得本文的内容对您的学习有所帮助,您可以微信:
分享到:
相关推荐
jQuery.isPlainObject-v1.10.2源码
$.blur,.mousedown,.change,.mouseenter,.click,.mouseleave,.dblclick,.mousemove,.error,.mouseout,.focus,.mouseover,.focusin,.mouseup,.focusout,.resize,.keydown,.scroll,.keypress,.select,.keyup,.submit,....
jQuery 1.4.1 速查表 -- Shawphy, 原作:G....get(index) index(subject) 数据缓存 data...$.isEmptyObject(obj) $.isPlainObject(obj) 字符串操作 $.trim(str) URL $.param(obj, [traditional]) 插件编写 $.error(message)
使用$.isPlainObject()函数检测对象是否为原始对象 使用$.contains()函数检测两个节点是否包含 使用$.param()进行数组元素序列化 使用函数$.extend()扩展工具函数 使用函数$.proxy()改变事件函数的作用域 使用...
$.data(ele,[key],[val])1.8- 队列控制 queue(e,[q]) dequeue([queueName]) clearQueue([queueName]) 插件机制 jQuery.fn.extend(object) jQuery.extend(object) 多库共存 jQuery.noConflict([ex]) 属性 ...
说明 jQuery中的isPlainObject() 函数用于判断指定参数是否是一个纯粹的对象,返回值为Boolean类型。...$.isPlainObject( object ) 参数说明: object:任意类型 需要进行判断的任意值。 $.isPlainObject({}); //
代码如下: /* *数组元素去重 */ if(typeof Array....i++){ if($.isPlainObject(this[i]) && $.isPlainObject(this[i+1])){ if(o2o(this[i],this[i+1])){ this.splice(i,1); } }else if($.isArray(this[i]) && $.is
但是有时候返回的是一个对象,json格式的数据,jQuery可以用这个方法循环遍历读出对象的值,假如这个对象名称是obj,循环遍历打印它的值: 代码如下: $.each(obj,function(key,val){ if($.isPlainObject(val) || $...
6.3.2 $.ajaxSetup()设置全局Ajax /181 6.4 Ajax中的全局事件/184 6.4.1 Ajax全局事件的基本概念/184 6.4.2 ajaxStart与ajaxStop全局事件/184 6.5 综合案例分析—用Ajax实现新闻点评即时更新/187 6.5.1 需求...
#10076:$.inArray crashes IE6 and Chrome if second argument is `null` or `undefined` CSS #6652:Remove filter:alpha(opacity=100) after animation #9572:Support -ms-transform in .css() method #10021:...
isPlainObject是Jquery1.4后提供的新方法,用于判断对象是否是纯粹的对象(通过 {} 或者 new Object 创建的)。
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. ...
是什么? :hear-no-evil_monkey: 非常简单的小型JS类型检查功能。 它完全支持TypeScript!... // import functions you want to use like so:import { isString , isDate , isPlainObject } from 'is-what' 首先
var isPlainObject = function isPlainObject(obj) { if (!obj || toStr.call(obj) !== '[object Object]') { return false; } var hasOwnConstructor = hasOwn.call(obj, 'constructor'); var ...