2010-12-01 75 views

回答

36

這應該爲你做它:

if(Object.prototype.toString.call(someObject) === '[object Object]') { 
    // do your iteration 
} 

的ECMAScript 5 8.6.2節如果你有興趣:

的[[類]]內部屬性的值由本規範針對每種內置對象定義。主對象的[[Class]]內部屬性的值可以是除「Arguments」,「Array」,「Boolean」,「Date」,「Error」,「Function」,「JSON」之外的任何字符串值。 ,「數學」,「數字」,「對象」,「RegExp」和「字符串」。 [[Class]]內部屬性的值用於內部區分不同類型的對象。請注意,除了通過Object.prototype.toString(見15.2.4.2)外,本規範沒有提供任何方法讓程序訪問該值。

+3

這不再保證在ES6中工作,因爲對象可以更改其Symbol.toStringTag屬性的值,該屬性用於生成Object.prototype.toString的返回值。請參閱http://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.prototype.tostring上的註釋它看起來像檢查某些東西現在可迭代的方式是查看它是否爲Symbol.iterator屬性被定義 – Ethan 2015-03-18 09:48:32

+0

`Object.prototype.toString`可以被覆蓋;因此,不安全。 – 2015-05-12 21:11:40

+3

如果它被覆蓋;該開發應該被槍殺!任何原型結構都可能會覆蓋它的方法,而如果你不能依賴它們,那麼它就沒用了。 – 2015-08-05 02:01:32

6

你也可以這樣做:

if (someObject.constructor == Object) { 
     // do your thing 
    } 

你可以閱讀更多關於它here

+2

如果someObject爲null,則它沒有構造函數,因此會引發TypeError。你真的想要上面的第一個答案。 – chilts 2013-06-13 03:19:40

-3

這個工作對我來說:

function isObject(o) { 
    try { 
     return ((typeof o == "object") && (o !== null) && (o.length === undefined)); 
    } catch (err) { 
     return false; 
    } 
} 
3

我覺得這樣的功能應是原生的,就像Array.isArray

Object.isObject = function(obj) { 
    return obj && obj.constructor === this || false; 
}; 

這一個不會進行函數調用或字符串比較,也不會引用全局對象(如Object),所以它應該很快。儘管如此,我認爲這不會用於性能密集型任務,但無論如何。

我並不完全相信這個名字......也許像Object.isLiteral會更好。

雖然它可能會導致主機對象出現問題,但我沒有準備好測試。

0

奇怪的是,我爲取決於如何的toString()被調用一個子類對象看到對的toString()不同的值:

Object.prototype.toString.call(aThing) 「[對象的對象]」

aThing.toString() 「ZmPopupMenu」

導致假陽性,所以我改成了更喜歡對象的toString():

var str = someObject.toString ? someObject.toString() : Object.prototype.toString.call(someObject); 
return str === '[object Object]'; 
-1

碰碰古老的線程,但它仍然顯示在搜索和人甚至引用它作爲一個新的類似的重複 - 仍然是這裏最頂級的答案是從正確(抱歉的人,沒有得罪)。

要檢查是否變量是以下應該使用的對象:

if (typeof variable === 'object') { 
    // do something 
} 

陣列也是對象,所以這會爲陣列是真實的太。 此外 - null也是一個有效的對象,因此上述將在null也返回true。 個人的時候,確實需要檢查,如果預期的變量是我一直用這個乏味重複公式「可行」對象:

if (variable && typeof variable === `object`) { 
    // do something 
} 

最後但並非最不重要:)請請請,請你幫個忙和唐不要使用任何庫來處理這麼簡單的事情。 Javascript是一種快速發展的語言,今天比昨天多得多,速度如此之快,以至於大多數圖書館的速度還不夠快。除此之外,正在從事規範工作的人都做得很好,大多數本地API都是乾淨的,正確的,與其他語言完美兼容。

1

假設你有一些testvar,並且想知道它是否是一個對象,但不是數組或null(它們都是Object類型)。你可以做以下

testVar instanceof Object && !Array.isArray(testVar) && testVar !== null 
-1

小要點,沒有真正優雅,但有效的

function _isObj(_obj){ 

    return (typeof _obj === "object" && JSON.stringify(_obj).indexOf("{") == 0); 

} 

小例子

function _isObj(_obj){ 
 

 
    return (typeof _obj === "object" && JSON.stringify(_obj).indexOf("{") == 0); 
 

 
} 
 

 
var p = document.createElement("p"); 
 
p.textContent = "undefined : " + _isObj(undefined); 
 
document.body.appendChild(p); 
 

 
p = document.createElement("p"); 
 
p.textContent = "null : " + _isObj(null); 
 
document.body.appendChild(p); 
 

 
p = document.createElement("p"); 
 
p.textContent = "boolean : " + _isObj(true); 
 
document.body.appendChild(p); 
 

 
p = document.createElement("p"); 
 
p.textContent = "function : " + _isObj(function(){}); 
 
document.body.appendChild(p); 
 

 
p = document.createElement("p"); 
 
p.textContent = "array : " + _isObj([]); 
 
document.body.appendChild(p); 
 

 
p = document.createElement("p"); 
 
p.textContent = "string : " + _isObj("{}"); 
 
document.body.appendChild(p); 
 
document.body.appendChild(p); 
 
document.body.appendChild(p); 
 

 
p = document.createElement("p"); 
 
p.textContent = "number : " + _isObj(1); 
 
document.body.appendChild(p); 
 
document.body.appendChild(p); 
 

 
p = document.createElement("p"); 
 
p.textContent = "object : " + _isObj({}); 
 
document.body.appendChild(p); 
 

 
p = document.createElement("p"); 
 
p.textContent = "another object : " + _isObj(p); 
 
document.body.appendChild(p);

希望這有助於