Reflect.ownKeys()
Reflect.ownKeys()
静态方法返回 target
对象自己的属性键的数组。
¥The Reflect.ownKeys()
static method returns an array of the target
object's own property keys.
Try it
语法
参数
返回值
例外情况
描述
¥Description
Reflect.ownKeys()
提供了检索对象的所有属性键的反射语义。这是在一次调用中获取所有自己的属性(可枚举和不可枚举、字符串和符号)的唯一方法,无需额外的过滤逻辑。例如,Object.getOwnPropertyNames()
采用 Reflect.ownKeys()
的返回值并仅过滤字符串值,而 Object.getOwnPropertySymbols()
仅过滤符号值。因为普通对象实现 [[OwnPropertyKeys]]
以返回符号键之前的所有字符串键,所以 Reflect.ownKeys(target)
通常等同于 Object.getOwnPropertyNames(target).concat(Object.getOwnPropertySymbols(target))
。但是,如果对象具有自定义 [[OwnPropertyKeys]]
方法(例如通过代理的 ownKeys
处理程序),则键的顺序可能会有所不同。
¥Reflect.ownKeys()
provides the reflective semantic of retrieving all property keys of an object. It is the only way to get all own properties – enumerable and not enumerable, strings and symbols — in one call, without extra filtering logic. For example, Object.getOwnPropertyNames()
takes the return value of Reflect.ownKeys()
and filters to only string values, while Object.getOwnPropertySymbols()
filters to only symbol values. Because normal objects implement [[OwnPropertyKeys]]
to return all string keys before symbol keys, Reflect.ownKeys(target)
is usually equivalent to Object.getOwnPropertyNames(target).concat(Object.getOwnPropertySymbols(target))
. However, if the object has a custom [[OwnPropertyKeys]]
method (such as through a proxy's ownKeys
handler), the order of the keys may be different.
Reflect.ownKeys()
调用 target
的 [[OwnPropertyKeys]]
对象内部方法。
¥Reflect.ownKeys()
invokes the [[OwnPropertyKeys]]
object internal method of target
.
示例
使用 Reflect.ownKeys()
¥Using Reflect.ownKeys()
Reflect.ownKeys({ z: 3, y: 2, x: 1 }); // [ "z", "y", "x" ]
Reflect.ownKeys([]); // ["length"]
const sym = Symbol.for("comet");
const sym2 = Symbol.for("meteor");
const obj = {
[sym]: 0,
str: 0,
773: 0,
0: 0,
[sym2]: 0,
"-1": 0,
8: 0,
"second str": 0,
};
Reflect.ownKeys(obj);
// [ "0", "8", "773", "str", "-1", "second str", Symbol(comet), Symbol(meteor) ]
// Indexes in numeric order,
// strings in insertion order,
// symbols in insertion order
规范
Specification |
---|
ECMAScript Language Specification # sec-reflect.ownkeys |
浏览器兼容性
BCD tables only load in the browser