Get to know MDN better
此頁面由社群從英文翻譯而來。了解更多並加入 MDN Web Docs 社群。
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
Object.keys() 方法會回傳一個由指定物件所有可列舉之屬性組成的陣列,該陣列中的的排列順序與使用 for...in 進行迭代的順序相同(兩者的差異在於 for-in 迴圈還會迭代出物件自其原型鏈所繼承來的可列舉屬性)。
物件,用以回傳其可列舉屬性。
回傳一個包含給定物件內所有可列舉屬性的字串陣列。
Object.keys() 回傳一個陣列,陣列中的各元素為直屬於 obj ,對應可列舉屬性名的字串。回傳結果的排序,與手動對物件屬性作迴圈迭代的結果排序相同。
如果想取得物件的所有屬性,包括非可列舉的屬性,請參閱 Object.getOwnPropertyNames().
在 ES5 中,如果這個方法的參數不是一個標準物件(例如原始型別),將會產生 TypeError錯誤。而在 ES2015,非物件的參數將會強制轉換成物件。
如需在原生不支援、較舊的環境中增加 Object.keys 的相容性,請複製以下片段:
請注意以上的代碼片段在 IE7 中( IE8 也有可能 ),從不同的 window 傳入物件將包含非可列舉的 key 。
較精簡的瀏覽器 Polyfill,請參閱 Javascript - Object.keys Browser Compatibility.
| ECMAScript® 2027 Language Specification # sec-object.keys |
Enable JavaScript to view this browser compatibility table.
This page was last modified on 2025年7月14日 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.