Object.getOwnPropertyNames()

Object.getOwnPropertyNames() 静态方法返回直接在给定对象中找到的所有属性(包括不可枚举属性,使用 Symbol 的属性除外)的数组。

¥The Object.getOwnPropertyNames() static method returns an array of all properties (including non-enumerable properties except for those which use Symbol) found directly in a given object.

Try it

语法

¥Syntax

js
Object.getOwnPropertyNames(obj)

参数

¥Parameters

obj

要返回其可枚举和不可枚举属性的对象。

返回值

¥Return value

与直接在给定对象中找到的属性相对应的字符串数组。

¥An array of strings that corresponds to the properties found directly in the given object.

描述

¥Description

Object.getOwnPropertyNames() 返回一个数组,其元素是与直接在给定对象 obj 中找到的可枚举和不可枚举属性相对应的字符串。数组中可枚举属性的顺序与对象属性上的 for...in 循环(或 Object.keys())公开的顺序一致。对象的非负整数键(可枚举和不可枚举)首先按升序添加到数组中,然后按插入顺序添加字符串键。

¥Object.getOwnPropertyNames() returns an array whose elements are strings corresponding to the enumerable and non-enumerable properties found directly in a given object obj. The ordering of the enumerable properties in the array is consistent with the ordering exposed by a for...in loop (or by Object.keys()) over the properties of the object. The non-negative integer keys of the object (both enumerable and non-enumerable) are added in ascending order to the array first, followed by the string keys in the order of insertion.

在 ES5 中,如果该方法的参数不是对象(原语),那么它将导致 TypeError。在 ES2015 中,非对象参数将被强制为对象。

¥In ES5, if the argument to this method is not an object (a primitive), then it will cause a TypeError. In ES2015, a non-object argument will be coerced to an object.

js
Object.getOwnPropertyNames("foo");
// TypeError: "foo" is not an object (ES5 code)

Object.getOwnPropertyNames("foo");
// ["0", "1", "2", "length"]  (ES2015 code)

示例

¥Examples

使用 Object.getOwnPropertyNames()

¥Using Object.getOwnPropertyNames()

js
const arr = ["a", "b", "c"];
console.log(Object.getOwnPropertyNames(arr).sort());
// ["0", "1", "2", "length"]

// Array-like object
const obj = { 0: "a", 1: "b", 2: "c" };
console.log(Object.getOwnPropertyNames(obj).sort());
// ["0", "1", "2"]

Object.getOwnPropertyNames(obj).forEach((val, idx, array) => {
  console.log(`${val} -> ${obj[val]}`);
});
// 0 -> a
// 1 -> b
// 2 -> c

// non-enumerable property
const myObj = Object.create(
  {},
  {
    getFoo: {
      value() {
        return this.foo;
      },
      enumerable: false,
    },
  },
);
myObj.foo = 1;

console.log(Object.getOwnPropertyNames(myObj).sort()); // ["foo", "getFoo"]

如果你只需要可枚举属性,请参阅 Object.keys() 或使用 for...in 循环(请注意,这还将返回沿着对象原型链找到的可枚举属性,除非后者使用 Object.hasOwn() 进行过滤)。

¥If you want only the enumerable properties, see Object.keys() or use a for...in loop (note that this will also return enumerable properties found along the prototype chain for the object unless the latter is filtered with Object.hasOwn()).

原型链上的项目未列出:

¥Items on the prototype chain are not listed:

js
function ParentClass() {}
ParentClass.prototype.inheritedMethod = function () {};

function ChildClass() {
  this.prop = 5;
  this.method = function () {};
}
ChildClass.prototype = new ParentClass();
ChildClass.prototype.prototypeMethod = function () {};

console.log(Object.getOwnPropertyNames(new ChildClass()));
// ["prop", "method"]

仅获取不可枚举属性

¥Get non-enumerable properties only

这使用 Array.prototype.filter() 函数从所有键(通过 Object.getOwnPropertyNames() 获得)的列表中删除可枚举键(通过 Object.keys() 获得),从而仅给出不可枚举键作为输出。

¥This uses the Array.prototype.filter() function to remove the enumerable keys (obtained with Object.keys()) from a list of all keys (obtained with Object.getOwnPropertyNames()) thus giving only the non-enumerable keys as output.

js
const target = myObject;
const enumAndNonenum = Object.getOwnPropertyNames(target);
const enumOnly = new Set(Object.keys(target));
const nonenumOnly = enumAndNonenum.filter((key) => !enumOnly.has(key));

console.log(nonenumOnly);

规范

Specification
ECMAScript Language Specification
# sec-object.getownpropertynames

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看