Object.getOwnPropertySymbols()

Object.getOwnPropertySymbols() 方法返回一个给定对象自身的所有 Symbol 属性的数组。

语法

Object.getOwnPropertySymbols(obj)

参数

obj
要返回 Symbol 属性的对象。

返回值

在给定对象自身上找到的所有 Symbol 属性的数组。

描述

Object.getOwnPropertyNames()本身不包含对象的 Symbol 属性,只包含字符串属性。

因为所有的对象在初始化的时候不会包含任何的 Symbol,除非你在对象上赋值了 Symbol 否则Object.getOwnPropertySymbols()只会返回一个空的数组。

示例

var obj = {};
var a = Symbol("a");
var b = Symbol.for("b");

obj[a] = "localSymbol";
obj[b] = "globalSymbol";

var objectSymbols = Object.getOwnPropertySymbols(obj);

console.log(objectSymbols.length); // 2
console.log(objectSymbols)         // [Symbol(a), Symbol(b)]
console.log(objectSymbols[0])      // Symbol(a)

规范

Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
Object.getOwnPropertySymbols
Standard Initial definition.
ECMAScript Latest Draft (ECMA-262)
Object.getOwnPropertySymbols
Draft  

浏览器兼容

Update compatibility data on GitHub
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
getOwnPropertySymbols Chrome Full support 38 Edge Full support 12 Firefox Full support 36 IE No support No Opera Full support 25 Safari Full support 9 WebView Android Full support 38 Chrome Android Full support 38 Firefox Android Full support 36 Opera Android Full support 25 Safari iOS Full support 9 Samsung Internet Android Full support 3.0 nodejs Full support 0.12

Legend

Full support  
Full support
No support  
No support

相关链接