/

The Object getOwnPropertySymbols() Method

The Object getOwnPropertySymbols() Method

In JavaScript, the getOwnPropertySymbols() method of the Object object allows you to obtain an array of symbols defined on an object.

Symbols are a feature introduced in ES2015, and this method was also added in ES2015.

Here is an example of how to use the getOwnPropertySymbols() method:

1
2
3
4
5
6
7
8
9
10
11
12
13
const dog = {}
const r = Symbol('Roger')
const s = Symbol('Syd')
dog[r] = {
name: 'Roger',
age: 6
}
dog[s] = {
name: 'Syd',
age: 5
}

Object.getOwnPropertySymbols(dog) //[ Symbol(Roger), Symbol(Syd) ]

By calling Object.getOwnPropertySymbols(dog), you will receive an array containing the symbols associated with the dog object, which in this case are Symbol(Roger) and Symbol(Syd).

Tags: JavaScript, Object, getOwnPropertySymbols(), symbols