JavaScript Object.getOwnPropertyNames()

定義和用法

Object.getOwnPropertyNames() 方法返回一個包含對象所有屬性名的數組。

Object.getOwnPropertyNames() 方法不會改變原始對象。

相關方法:

Object.defineProperty() 添加或修改屬性。

Object.defineProperties() 添加或修改多個屬性。

Object.getOwnPropertyNames() 返回對象的所有屬性名。

Object.getOwnPropertyDescriptor() 返回屬性的描述符。

Object.getOwnPropertyDescriptors() 返回對象所有屬性的描述符。

實例

// 創建一個對象
const person = {
  firstName: "Bill",
  lastName: "Gates",
  age: 50,
  eyeColor: "blue"
};
// 獲取所有屬性名
let props = Object.getOwnPropertyNames(person);

親自試一試

語法

Object.getOwnPropertyNames(object)

參數

參數 描述
object 必需。目標對象。

返回值

類型 描述
Array 包含對象所有屬性名的數組。

瀏覽器支持

Object.getOwnPropertyNames() 是 ECMAScript5 (ES5) 的特性。

自 2013 年 7 月起,所有現代瀏覽器均完全支持 ES5(JavaScript 2009):

Chrome Edge Firefox Safari Opera
Chrome 23 IE/Edge 11 Firefox 21 Safari 6 Opera 15
2012 年 9 月 2012 年 9 月 2013 年 4 月 2012 年 7 月 2013 年 7 月