[英]How to detect if a variable is a pure javascript object
我有一个 function 我需要将 object 传递给。 我使用typeof
运算符在处理之前进行检查。 但是查看此链接,似乎许多 javascript 实例(例如数组或正则表达式)都被键入为 Objects 。
我需要我的论点是一个纯粹的 object(像这样: {key: value, . . .}
)。
他们有什么办法可以检查一个变量是否是纯 object,而不必为每个 Object 实例运行特定测试,比如Array.isArray()
?
为了达到预期的结果,请使用以下查找构造函数名称的选项来检查变量是否为纯 javascript 对象
根据 MDN,
所有对象(使用 Object.create(null) 创建的对象除外)都将具有构造函数属性。 未显式使用构造函数(即对象和数组文字)创建的对象将具有指向该对象的基本对象构造函数类型的构造函数属性。
有关构造函数属性的更多详细信息,请参阅此链接 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor
var x = {a:1,b:2}; var y = [1,2,3]; console.log(x.constructor.name === "Object")//x.constructor.name is Object console.log(y.constructor.name === "Object")//y.constructor.name is Array
您可以检查原型:
function isPureObject(input) { return null !== input && typeof input === 'object' && Object.getPrototypeOf(input).isPrototypeOf(Object); } console.log(isPureObject({})); console.log(isPureObject(new Object())); console.log(isPureObject(undefined)); console.log(isPureObject(null)); console.log(isPureObject(1)); console.log(isPureObject('a')); console.log(isPureObject(false)); console.log(isPureObject([])); console.log(isPureObject(new Array())); console.log(isPureObject(() => {})); console.log(isPureObject(function () {}));
最短的版本
const isObj = o => o?.constructor === Object;
由于“普通”或“纯”的定义,普通对象的检测有点问题。 我使用以下方法来检测普通对象。 我从不使用x instanceof NodeList
类型验证,因为它们不适用于跨框架情况。 (每个窗口或框架都有自己的实例)
我认为这是检测普通物体的最简单和最有效的方法。
function isPlainObject(o) { var c = Object.prototype.toString.call(o) == '[object Object]' && o.constructor && o.constructor.name=="Object"; return c === true; } function myTestFunction(){ /* ... */ } class myTestClass{ /* ... */ } console.log( isPlainObject({"a":1,"b":2}) ); //true console.log( isPlainObject({}) ); //true console.log( isPlainObject(null) ); //false console.log( isPlainObject("my string") ); //false console.log( isPlainObject("") ); //false console.log( isPlainObject([1,2,3]) ); //false console.log( isPlainObject(document.querySelectorAll("*")) ); //false console.log( isPlainObject(new RegExp(/[az]+/)) ); //false console.log( isPlainObject(myTestFunction) ); //false console.log( isPlainObject(new myTestFunction()) ); //false console.log( isPlainObject(myTestClass) ); //false console.log( isPlainObject(new myTestClass()) ); //false
如果您只想检查“普通”纯对象(由{...}
、 JSON.parse(...)
等创建的对象),那么这个 function 将起作用
function is_pure_object(val) {
return val ? Object.getPrototypeOf(val)==Object.prototype : false
}
如果您还需要处理没有原型的对象(非常罕见,仅由Object.create(null)
创建),那么您需要进行额外的检查:
function is_pure_object2(val) {
if (!val)
return false
let proto = Object.getPrototypeOf(val)
return proto == Object.prototype || proto == null
}
有关其他解决方案的说明:
val.constructor
是不可靠的:
{constructor: 1}
这样的值上失败Object.create(null)
上失败Object.prototype.constructor
会出错Object.getPrototypeOf(val).isPrototypeOf(Object)
也不理想:
Object.create(null)
错误Object.prototype.isPrototypeOf
被修改会出错 ( Object.prototype
被修改的可能性不大,但我之前处理过)
使用 lodash 库,您可以使用以下 function 实现此目的:
const isPureObject = _.isPlainObject({ foo: 'Zebra' });
expect(isPureObject).to.be.true;
const isMapPureObject = _.isPlainObject(new Map());
expect(isMapPureObject).to.be.false;
可以在此处找到官方文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.