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.
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. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 |
完整支持
不支持
Object
Object.assign()
Object.create()
Object.defineProperties()
Object.defineProperty()
Object.entries()
Object.freeze()
Object.fromEntries()
Object.getOwnPropertyDescriptor()
Object.getOwnPropertyDescriptors()
Object.getOwnPropertyNames()
Object.getOwnPropertySymbols()
Object.getPrototypeOf()
Object.is()
Object.isExtensible()
Object.isFrozen()
Object.isSealed()
Object.keys()
Object.preventExtensions()
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.seal()
Object.setPrototypeOf()
Object.values()
Function