Object.getOwnPropertySymbols()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Object.getOwnPropertySymbols()
静态方法返回一个包含给定对象所有自有 Symbol 属性的数组。
尝试一下
const object1 = {};
const a = Symbol("a");
const b = Symbol.for("b");
object1[a] = "localSymbol";
object1[b] = "globalSymbol";
const objectSymbols = Object.getOwnPropertySymbols(object1);
console.log(objectSymbols.length);
// Expected output: 2
语法
js
Object.getOwnPropertySymbols(obj)
参数
obj
-
要返回 Symbol 属性的对象。
返回值
在给定对象找到的所有自有 Symbol 属性的数组。
描述
与 Object.getOwnPropertyNames()
类似,你可以将给定对象的所有符号属性作为 Symbol 数组获取。请注意,Object.getOwnPropertyNames()
本身不包含对象的 Symbol 属性,只包含字符串属性。
因为所有的对象在初始化的时候不会包含任何自有的 Symbol 属性,除非你在对象上分配了 Symbol 属性,否则 Object.getOwnPropertySymbols()
只会返回一个空的数组。
示例
使用 Object.getOwnPropertySymbols()
js
const obj = {};
const a = Symbol("a");
const b = Symbol.for("b");
obj[a] = "localSymbol";
obj[b] = "globalSymbol";
const objectSymbols = Object.getOwnPropertySymbols(obj);
console.log(objectSymbols.length); // 2
console.log(objectSymbols); // [Symbol(a), Symbol(b)]
console.log(objectSymbols[0]); // Symbol(a)
规范
Specification |
---|
ECMAScript® 2025 Language Specification # sec-object.getownpropertysymbols |
浏览器兼容性
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
getOwnPropertySymbols |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.