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

같이 보기