Object.getOwnPropertySymbols() method returns an array of all symbol properties found directly upon a given object.

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

句法

Object.getOwnPropertySymbols(obj)
					

参数

obj

The object whose symbol properties are to be returned.

返回值

An array of all symbol properties found directly upon the given object.

描述

类似于 Object.getOwnPropertyNames() , you can get all symbol properties of a given object as an array of symbols. Note that Object.getOwnPropertyNames() itself does not contain the symbol properties of an object and only the string properties.

As all objects have no own symbol properties initially, Object.getOwnPropertySymbols() returns an empty array unless you have set symbol properties on your object.

范例

Using 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)
					

规范

规范
ECMAScript (ECMA-262)
The definition of 'Object.getOwnPropertySymbols' in that specification.

浏览器兼容性

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. 更新 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 38 Edge 12 Firefox 36 IE No Opera 25 Safari 9 WebView Android 38 Chrome Android 38 Firefox Android 36 Opera Android 25 Safari iOS 9 Samsung Internet Android 3.0 nodejs 0.12

图例

完整支持

完整支持

不支持

不支持

另请参阅

元数据

  • 最后修改: