이동:

번역 작업 진행중입니다.

이 용어집 페이지는 "symbol"이라는 자료형과 symbol 자료형의 인스턴스를 생성하는 "Symbol ()"이라는 클래스와 같은 함수를 모두 설명합니다.

자료형 "symbol"은 품질을 가진 원시 자료형으로 익명의 개체 속성을 만들 수 있습니다. 이 자료형은 클래스 또는 객체 유형의 내부 사용을 위한 객체 속성의 private 키로 사용됩니다. 예를 들어, symbol타입의 키는 다양한 내장 JavaScript 객체에 존재하는데 사용자 지정 클래스역시 이러한 방식으로 전용 멤버를 만들수 있습니다. symbol 자료형은 특정한 목적을 가지고 사용되기에 적합하고 다양한 목적으로 사용하기 부적절 합니다. an instance of Symbol can be assigned to an L-value, it can be examined for identity, and that's all; no other operators apply. (이것을 "Number"의 인스턴스 (예 : 정수 "42")와 비교해보십시오. 이 정수에는 값을 다른 유형과 비교하거나 결합하는 연산자가 있습니다)

A value having the data type "symbol" can be referred to as a "symbol value."  In the JavaScript run-time environment, a symbol value is created by invoking the function Symbol(), which dynamically produces an anonymous, unique value. A symbol may be used as an object property.

 

"symbol"자료형인 값은 "symbol value"라고 할 수 있습니다. JavaScript 런타임 환경에서 기호 값은 익명의 고유 값을 동적으로 생성하는 Symbol () 함수를 호출하여 만들어집니다. 심볼은 객체 속성으로 사용될 수 있습니다.

var  myPrivateMethod  = Symbol();
this[myPrivateMethod] = function() {...};

When a symbol value is used as the identifier in a property assignment, the property (like the symbol) is anonymous; and also is non-enumerable. Because the property is non-enumerable, it will not show up as a member in the loop construct "for( ... in ...)", and because the property is anonymous, it will not show up in the result array of "Object.getOwnPropertyNames()". The property can be accessed by using the original symbol value that created it, or by iterating on the result array of "Object.getOwnPropertySymbols()". In the prior code example, access to the property will be through the value that was stored in the myPrivateMethod variable.

The built-in function "Symbol()" is an incomplete class that returns a symbol value when called as a function, that throws an error upon attempts to use it as a constructor with the syntax "new Symbol()", that has static methods for accessing JavaScript's global symbol table, and that has static properties for addressing certain symbols that are present in commonly used objects. The creation of symbol values by the Symbol() function was explained above. The throwing of an error upon attempts to use Symbol() as a constructor is explained as a precaution against the accidental creation of an object that might cause confusion. The methods that access the global symbol registry are "Symbol.for()" and "Symbol.keyFor()"; these mediate between the global symbol table (or "registry") and the run-time environment. The symbol registry is mostly built by JavaScript's compiler infrastructure, and the symbol registry's content is not available to JavaScript's run-time infrastructure, except through these reflective methods. The method Symbol.for("tokenString") returns a symbol value from the registry, and Symbol.keyFor(symbolValue) returns a token string from the registry; each is the other's inverse, so the following is true:

Symbol.keyFor(Symbol.for("tokenString")) == "tokenString"; // true

The Symbol class has some static properties that have the ironic effect of naming the anonymous. There are only a few of these; they are for some so-called "well known" symbols. These are symbols for some selected method properties that are found in certain built-in objects. The exposure of these symbols makes it possible to have direct access to these behaviors; such access might be useful, for example, in the definition of a custom class. Examples of well-known symbols are: "Symbol.iterator" for array-like objects, and "Symbol.search" for string objects. 

The Symbol() function and the symbol values it creates might be useful to programers designing a custom class. Symbol values provide a way by which custom classes can create private members, and maintain a symbol registry that pertains just to that class. A custom class can use symbol values to create "own" properties that are shielded from unwanted, casual discovery. Within the class definition, the dynamically created symbol value is saved to a scoped variable, available only privately within the class definition. There is no token string; the scoped variable plays the equivalent role of a token.

In some programming languages the symbol data type is referred to as an "atom."  

In JavaScript, Symbol is a primitive value.

Symbol can have an optional description, but for debugging purposes only.

Symbol type is a new feature in ECMAScript 2015 and there is no ECMAScript 5 equivalent for symbol.

Symbol("foo") !== Symbol("foo")
const foo = Symbol()
const bar = Symbol()
typeof foo === "symbol"
typeof bar === "symbol"
let obj = {}
obj[foo] = "foo"
obj[bar] = "bar"
JSON.stringify(obj) // {}
Object.keys(obj) // []
Object.getOwnPropertyNames(obj) // []
Object.getOwnPropertySymbols(obj) // [ Symbol(), Symbol() ]

Learn more

General knowledge

문서 태그 및 공헌자

이 페이지의 공헌자: limkukhyun
최종 변경: limkukhyun,