JavaScript(JS) object method - getOwnPropertySymbols

The getOwnPropertySymbols() method is a built-in method of the JavaScript Object prototype. It returns an array of all symbol properties found directly upon a given object.

Here's the syntax:

Object.getOwnPropertySymbols(obj)
So‮.www:ecru‬theitroad.com

where obj is the object to be inspected. This method takes only one argument, which is the object whose symbol properties we want to retrieve.

Symbol properties are a new feature in ECMAScript 2015 (ES6). They are a new type of primitive data that allow the creation of "hidden" properties in an object. Symbol properties are unique and immutable, and they can be used as keys in object properties.

Here's an example that shows how to use the getOwnPropertySymbols() method:

const myObj = {};
const sym1 = Symbol("key1");
const sym2 = Symbol("key2");

myObj[sym1] = "value1";
myObj[sym2] = "value2";

const symbols = Object.getOwnPropertySymbols(myObj);

console.log(symbols); // [ Symbol(key1), Symbol(key2) ]

In this example, we create an empty object myObj and two symbol properties sym1 and sym2. We then use these symbols as keys in the myObj object. Finally, we use Object.getOwnPropertySymbols() to retrieve an array of all the symbol properties found directly upon myObj.

Note that getOwnPropertySymbols() only returns the symbol properties that are directly attached to an object. It does not return symbol properties from the object's prototype chain. To get all symbol properties (including those in the prototype chain), you can use the Reflect.ownKeys() method.