JavaScript Symbol type: (non-string object keys) - Stack Overflow

What is the "Symbol" javascript type as mentioned in this ECMAScript 6 draft specification?T

What is the "Symbol" javascript type as mentioned in this ECMAScript 6 draft specification?

To quote the spec:

The Symbol type is the set of all non-String values that may be used as the key of an Object property.

Each possible Symbol values is unique and immutable.

Symbol values have a single observable attribute called [[Private]] whose immutable value is either true or false. A private symbol is a Symbol value whose [[Private]] attribute has the value true.

I thought object keys were strings only, and I'm not alone. To quote this accepted SO answer:

…object keys are always strings…

Could you explain what the symbol type is, and demonstrate its usage. I'm trying to make sense of the spec.

Thanks!

What is the "Symbol" javascript type as mentioned in this ECMAScript 6 draft specification?

To quote the spec:

The Symbol type is the set of all non-String values that may be used as the key of an Object property.

Each possible Symbol values is unique and immutable.

Symbol values have a single observable attribute called [[Private]] whose immutable value is either true or false. A private symbol is a Symbol value whose [[Private]] attribute has the value true.

I thought object keys were strings only, and I'm not alone. To quote this accepted SO answer:

…object keys are always strings…

Could you explain what the symbol type is, and demonstrate its usage. I'm trying to make sense of the spec.

Thanks!

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Jul 20, 2013 at 7:17 Web_DesignerWeb_Designer 74.7k93 gold badges210 silver badges267 bronze badges 1
  • 1 That's ES6 spec, not sure but see if this helps: github./dherman/tc39-codex-wiki/blob/master/data/es6/symbols/… – elclanrs Commented Jul 20, 2013 at 7:20
Add a ment  | 

3 Answers 3

Reset to default 6

I thought object keys were strings only

You're right, but that was true for EcmaScript 5 only. ES 6 / harmony is a draft for something new!

I'm trying to make sense of the spec

It's a draft only, rapidly changing. How symbols are used and whether or how they can be created by arbitrary scripts does not seem to have settled yet (scan through the versions for changes).

If you scroll down to the very end of that document (even below Annex F), you for example will see a Section 8.4.4: Symbol Exotic Objects that has been moved out there. It states

Exotic Symbol objects provide alternative definitions for all of the essential internal methods.

You can see them used at section 8.1.7.4 Well-Known Symbols and Intrinsics for example. For proposed uses (and still existing problems / open questions) of Symbol constructors have a look at these strawman pages or this wiki site.

Symbol is a new addition to the language proposed as part of ECMAScript 6:

Current work on ECMAScript™

Work on future ECMAScript™ editions continues as part of the previously announced ECMAScript™ "Harmony" project. More details of the current work on ECMAScript "Harmony" are described on this Wiki. A sixth edition of the standard is currently under development with a target date of December 2013 for pletion.

We use symbols to make the properties or methods of an object private. So we hide the details and show only the essentials. It is called abstraction.

How to implement this: Let's create a simple class with "radius" property

class Circle {
    constructor(radius) {
        this.radius = radius; 
    }}

A symbol is essentially a unique identifier. Every time we call this function we get a unique identifier. It is not a constructor function,though.

Symbol()===Symbol() //will be false

Implementation:

const _radius=Symbol()
class Circle {
    constructor(radius) {
    this[_radius] = radius; //since property name starts with _, we use bracket notation
        }
         }

now test this. Create an instance of Circle:

const c=new Circle;
console.log(Object.getOwnPropertyNames(c))// you will see a number on the console.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744282521a4566640.html

相关推荐

  • JavaScript Symbol type: (non-string object keys) - Stack Overflow

    What is the "Symbol" javascript type as mentioned in this ECMAScript 6 draft specification?T

    8天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信