我有一个值并想知道它是否是一个可迭代的对象字面量,在迭代它之前。
我该怎么做?
这应该能为你解决问题:
if( Object.prototype.toString.call( someObject ) === '[object Object]' ) {
// do your iteration
}
如果您有兴趣,可以阅读 ECMAScript 5 第8.6.2节:
每种内置对象的[[Class]]内部属性的值在此规范中都有定义。主机对象的[[Class]]内部属性的值可以是除“Arguments”、“Array”、“Boolean”、“Date”、“Error”、“Function”、“JSON”、“Math”、“Number”、“Object”、“RegExp”和“String”之外的任何字符串值。[[Class]]内部属性的值在内部用于区分不同类型的对象。请注意,本规范没有为程序提供任何访问该值的方法,除非通过Object.prototype.toString(参见15.2.4.2)。
Object.prototype.toString
可被覆盖,因此不安全。 - Frederik Krautwaldvar str; toString.call(str)
如果值为undefined或null,则在IE <9和某些移动设备中会返回“object”。在应用此条件之前,先检查undefined和null。 - RAMM-HDRObject.prototype.toString = function() { return '[object Object]' }
也可以。然后从那时起,Object.prototype.toString.call( someObject ) === '[object Object]'
将返回true。 - Frederik Krautwaldinstanceof
运算符更好,因为它支持空值并且可以检查完整的原型层次结构。 - Aram Kocharyan([]) instanceof Object
返回 true。现在可以使用 someObject?.constructor === Object
。 - Andre Figueiredo我认为像这样的函数应该是原生的,就像Array.isArray
一样:
Object.isObject = function(obj) {
return obj && obj.constructor === this || false;
};
Object
),因此应该非常快。虽然我认为这不会在性能密集型任务中使用,但无论如何都可以试试。Object.isLiteral
这样的名称会更好。|| false
这部分是非常不必要的。 - Mulanobj === null
,它会返回 null
。你可以使用 return obj != null && object.constructor === this;
。 - MaxArtreturn !!(obj && obj.constructor === this)
。 - Aram Kocharyanreturn !!obj && obj.constructor === this
,这样更清晰明了。值得明确的是,你实际上只是在执行 return !!obj && obj.constructor === Object
,以防有人想在其他地方使用此代码。我看到了你的速度注释,但无论如何它都非常快。 - Ryan Taylorobj instanceof Object
或typeof obj === "object"
,那么像new Number(3)
和数组([1,2,3]
)这样的东西会得到错误的结果。o.constructor === Object
很好,但是有一种奇怪的边界情况是Object.create(null)
- 事实上,它确实会给您一个普通对象,尽管不是用"正常"的方式创建的。从概念上讲,它提供了一个有效的、普通的、未装饰的对象。我们通过Object.getPrototypeOf(o) === null
检查这种情况,这只对上述未装饰对象类型成立。
!!o
将null
或undefined
转换为false
。人们在上面抱怨它,老实说,o && ...
更加简洁,除非您进行序列化,否则没有关系。尽管如此,我还是包含了它。function isObject(o) {
return o && o.constructor === Object
}
function isObject1(o) {
return !!o && o.constructor === Object
}
function isObject2(o) {
// edge case where you use Object.create(null) –– which gives an object {} with no prototype
return !!o && (Object.getPrototypeOf(o) === null || o.constructor === Object)
}
!!
的人,我现在认为Boolean(o)
更清晰地表达了意图(同样的原因是我不使用一元运算符将字符串转换为数字,而是使用Number(str)
)。 - Dexygentestvar
,想要确认它是否为对象,但不是数组或null(因为这两者都属于类型Object)。你可以进行如下操作:testVar instanceof Object && !Array.isArray(testVar) && testVar !== null
const testVar = new Number(2)
是什么意思? - Ryan Taylor其实,当有像Lodash和Underscore这样优秀的库存在时,你就不需要自己检查所有内容或编写自己的代码了。
在 Lodash 中,你可以通过 isPlainObject 函数轻松检查,例如:
_.isPlainObject({'a': 12});
奇怪的是,当调用toString()方法时,我发现子类对象的toString()返回值不同:
Object.prototype.toString.call(aThing)
"[object Object]"
aThing.toString()
"ZmPopupMenu"
这会导致一个错误的正向结果,因此我修改它以优先使用对象的toString()方法:
var str = someObject.toString
? someObject.toString()
: Object.prototype.toString.call(someObject);
return str === '[object Object]';
toString
属性被重写为返回不同值的函数,例如 alert
,那么它将显示该值。然而,如果 anObject
确实是一个对象,Object.prototype.toString.call( anObject )
将始终返回字符串 [object Object]
。要使 Object.prototype.toString
返回不同的内容,必须重写 Object
的原型 toString
。 - Frederik KrautwaldObj && typeof Obj === 'object' && !Array.isArray(Obj);
function typeOfObj(obj) {
if(typeof obj === 'function' && String(obj).search('class') === 0) return 'class';
return Object.prototype.toString.call(obj).replace(/.*\w*\s(\w*).*/,'$1').toLowerCase();
}
if(typeof obj !== 'undefined' && typeOfObj(obj) == 'object') true;
const isObject = (x) => {
return Object.prototype.toString.call(x) == "[object Object]";
}
对我来说可以。