检查该值是否为对象字面值? [英] Check that value is object literal?
问题描述
我有一个值,想要知道它是否是一个可迭代的对象文字,然后再迭代它。
I have a value and want to know if it's an iteratable object literal, before I iterate it.
我该怎么做?
推荐答案
这应该适合你:
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之一之外的任何String值。 ,数学,数字,对象,RegExp和字符串。内部使用[[Class]]内部属性的值来区分不同类型的对象。请注意,除了通过Object.prototype.toString(参见15.2.4.2)之外,本规范没有为程序提供访问该值的任何方法。
The value of the [[Class]] internal property is defined by this specification for every kind of built-in object. The value of the [[Class]] internal property of a host object may be any String value except one of "Arguments", "Array", "Boolean", "Date", "Error", "Function", "JSON", "Math", "Number", "Object", "RegExp", and "String". The value of a [[Class]] internal property is used internally to distinguish different kinds of objects. Note that this specification does not provide any means for a program to access that value except through Object.prototype.toString (see 15.2.4.2).
这篇关于检查该值是否为对象字面值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!