Object.getOwnPropertySymbols()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Object.getOwnPropertySymbols() 静的メソッドは、与えられたオブジェクト上で直接見つかるシンボルプロパティすべての配列を返します。

試してみましょう

const object1 = {};
const a = Symbol("a");
const b = Symbol.for("b");

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

const objectSymbols = Object.getOwnPropertySymbols(object1);

console.log(objectSymbols.length);
// Expected output: 2

構文

js
Object.getOwnPropertySymbols(obj)

引数

obj

シンボルプロパティが返されるオブジェクトです。

返値

与えられたオブジェクト上で直接見つかるシンボルプロパティすべての配列です。

解説

Object.getOwnPropertyNames() と同様、与えられたオブジェクトのすべてのシンボルプロパティをシンボルの配列として取得することができます。 Object.getOwnPropertyNames() 自体はオブジェクトのシンボルプロパティを含まず、文字列プロパティのみを含むことに注意して下さい。

すべてのオブジェクトが最初に自身のシンボルプロパティを持っているとは限らないので、 Object.getOwnPropertySymbols() は、オブジェクトにシンボルプロパティを設定しない限りは空の配列を返します。

getOwnPropertySymbols の使用

js
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
ECMAScript® 2025 Language Specification
# sec-object.getownpropertysymbols

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
getOwnPropertySymbols

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

関連情報