jq1.4中新增了个静态方法$.isPlainObject,见http://api.jquery.com/jquery.isPlainObject/
。对于通过字面量定义的对象和new Object的对象返回true,new Object时传参数的返回false,如:
var p1 = new Object;
p1.name = 'tom';
var o1 = new Object('aa');
console.log($.isPlainObject({name:'Jack'})); //-> true
console.log($.isPlainObject(p1)); //-> true
console.log($.isPlainObject('aa')); //-> false
console.log($.isPlainObject(o1)); //-> false
$.isPlainObject(o1)返回也是false,见: 关于 new Object 时传参的一些细节
通过字面量定义的对象,其构造器是Object,很容易证明。
var obj = {};
console.log(obj.constructor === Object); //-> true
感觉1.4新加的这个方法用处不大,只能判断由Object构造器生成的对象(传参数时例外),甚至一度以为这只是在jq内部用而已,但偏偏公开了,挂在了$上。当自定义一个类(构造器),用$.isPlainObject判断new自定义类的对象时将返回false。
//自定义类(构造器),创建一个对象p
function Person(){this.name='jack'}
var p = new Person();
$.isPlainObject(p); //-> false
因此想写一个对于通过字面量定义及通过自定义类(构造器)创建的对象都返回true的方法。
function myIsPlainObject( obj ){
return Object.prototype.toString.call(obj)==='[object Object]';
}
测试:
function Person(name){
this.name=name;
}
var p = new Person('jack');
var o = {name:'tom'};
console.log(myIsPlainObject(p)); //-> true
console.log(myIsPlainObject(o)); //-> true
都返回true,一切貌似很顺利。偏偏IE中window/document/document.body/HTMLElement/HTMLCollection/NodeList/也返回true。
即IE中Object.prototype.toString.call以上对象返回的字符串也是'[object Object]',不知IE为何这样实现,Firefox/Chrome/Safari/Opera则不然。
修改下:
function myIsPlainObject( obj ){
return 'isPrototypeOf' in obj && Object.prototype.toString.call(obj)==='[object Object]';
}
这里判断obj是否具有isPrototypeOf属性,isPrototypeOf是挂在Object.prototype上的。通过字面量或自定义类(构造器)创建的对象都会继承该属性方法,在有约定的情况下,这个方法可以使用。
console.log(myIsPlainObject(window)); //-> false
console.log(myIsPlainObject(document)); //-> false
但不遵守约定的情况下,仍然失败,如在window上人为的
添加isPrototypeOf属性
window.isPrototypeOf = '';
console.log(myIsPlainObject(window)); //-> true
最后可以参考cai兄的讨论:http://cmc3.cn/n/2010/05/01/156.html
分享到:
相关推荐
jQuery.isPlainObject-v1.10.2源码
这个方法的作用是为了跟其他的 JavaScript对象如 null,数组,宿主对象(documents),DOM 等作区分,因为这些用 typeof 都会返回object。 使用 语法: $.isPlainObject( object ) 参数说明: object:任意类型 ...
isPlainObject是Jquery1.4后提供的新方法,用于判断对象是否是纯粹的对象(通过 {} 或者 new Object 创建的)。
我正寻找: 一种检查任何类型(包括非原始类型)的简单方法能够检查对象是普通对象{}还是特殊对象(例如类实例) ! 让TypeScript在检查时自动知道值的类型那is-what ! (很棒的文字游戏 :grinning_face_with_big_...
不同上下文的isPlainObject测试测试具有不同全局上下文的构造函数instanceof构造函数问题npm installnpm start
使用$.isPlainObject()函数检测对象是否为原始对象 使用$.contains()函数检测两个节点是否包含 使用$.param()进行数组元素序列化 使用函数$.extend()扩展工具函数 使用函数$.proxy()改变事件函数的作用域 使用...
但是有时候返回的是一个对象,json格式的数据,jQuery可以用这个方法循环遍历读出对象的值,假如这个对象名称是obj,循环遍历打印它的值: 代码如下: $.each(obj,function(key,val){ if($.isPlainObject(val) || $...
其次详细讲解了jQuery的各种选择器、jQuery操作DOM的方法、jQuery中的事件与应用、jQuery中的动画和特效、Ajax在jQuery中的应用,以及各种常用的jQuery插件的使用方法和技巧,所有这些知识点都配有完整的示例(包括...
安装$ npm install is-plain-obj用法import isPlainObject from 'is-plain-obj' ;isPlainObject ( { foo : 'bar' } ) ;//=> trueisPlainObject ( new Object ( ) ) ;//=> trueisPlainObject ( Object . create ( ...
用法与es模块import { isPlainObject } from 'is-plain-object' ; 或与commonjs const { isPlainObject } = require ( 'is-plain-object' ) ; 由Object构造函数或Object.create(null)创建时为true 。 ...
深度前端系列JavaScript :heavy_check_mark:然后,抓住,最后和静态方法解决,拒绝,所有,种族,全部解决 :heavy_check_mark: :heavy_check_mark:包含on,off,prependListener,setMaxListeners等方法 :heavy_...
代码如下: /* *数组元素去重 */ 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
恩布洛阿德希姆 安装 ember install ember-lodash-es-shim 用法 import isPlainObject from 'lodash/isPlainObject' ; 要了解有关lodash以及如何使用它的更多信息,请访问 Github页面。
annexe 一个通用的模块化Typescript实用程序库,可为您的现代浏览器进行日常编码。... @ annexe / isplainobject @ annexe / isset @ annexe / isstring @ annexe / istypedarray @ annexe / isweakmap @
var isPlainObject = function isPlainObject(obj) { if (!obj || toStr.call(obj) !== '[object Object]') { return false; } var hasOwnConstructor = hasOwn.call(obj, 'constructor'); var ...
#9897:try-catch isPlainObject detection #10076:$.inArray crashes IE6 and Chrome if second argument is `null` or `undefined` CSS #6652:Remove filter:alpha(opacity=100) after animation #9572:...
.src ├──实用程序├──actionTypes.js ├──isPlainObject.js ├──warning.js ├──applyMiddleware.js ├──bindActionCreators.js ├──CombineReducers.js ├──compose.js ├──createStore.js └─...
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. ...