What are the possible scenarios for using the new JavaScript character data type?

I just stumbled upon the documentation for a new (suggested for ES6, but already implemented in Firefox, Chrome and Opera) data type in JavaScript, Symbol :

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol http://people.mozilla.org/~jorendorff/es6-draft.html#sec-symbol-objects

I read about it, but I just can't think of a possible use case.

The documentation says:

A symbol is a unique and immutable data type and can be used as an identifier for the properties of an object.

OK, well, let's say I do, as the documentation says:

 obj[Symbol("a")] = "a"; 

but since Symbol ('a') always returns a unique value (object) and:

Characters are not displayed for ... in iterations.

How to get my property from obj ?

 var obj = { normalProperty: 'just a string' }; obj[Symbol('a')] = 'a'; document.getElementById('retrieve').addEventListener('click', function() { document.write('Stringified object:' + JSON.stringify(obj) + '<br/><br/>'); document.write('Trying to get Symbol-property value, aaaand...: <br/>'); document.write(obj[Symbol('a')]); // undefined }, false); 
 <button id="retrieve">Retrieve obj property</button> 

Edit

Of course you can get it like this:

 var x = Symbol('a'); obj[x] = 'a'; obj[x]; // "a" 

but what is the purpose of doing this?

Thank you in advance:)

+11
source share
2 answers

After reading the documentation and playing a little with this type of Symbol in chrome, it seems that Symbol is a way to determine the name, not the value, and the fact that the properties defined using the symbols are not visible using for..in , Object.getOwnPropertyNames() or JSON.stringify() makes characters useful for metadata properties:

 // define metadata symbols var Metadata = { Date: Symbol('Message date') }; var email = function(recipient, message) { this.Recipient = recipient; this.Message = message; this[Metadata.Date] = new Date(); }; var email1 = new email('@Me', 'test'); JSON.stringify(email1); // { // Recipient: '@Me', // Message: 'test' // } // Date is still accessible using email1[Metadata.Date]; // Thu Nov 27 2014 16:50:00 GMT+0000 // Debugging in Console: // { // Recipient: '@Me', // Message: 'test' // Symbol(Message date): Thu Nov 27 2014 16:50:00 GMT+0000 // } 

Symbols can be made global using the Symbol.for function, so metadata names can be created once and used in all project files.

Accessing a value using a symbol requires a reference to the symbol at creation. Each call to Symbol() creates a new one, even if the same description is used:

 var a = Symbol('a'); var b = Symbol('a'); a != b // and a != Symbol('a') 

but by creating a symbol using Symbol.for , it will be registered in the global registry and the description will become key, since only one symbol with the same key will exist in the global registry:

 var a = Symbol.for('a'); var b = Symbol.for('a'); a == b // and a == Symbol.for('a') 
+12
source

They greatly help us to have name conflicts. Anytime you want to create a property in a unique way, when you need to find a symbol.

Look at my example

 const bert = Symbol('Bert'); 

'Bert'

Note: this is not a value, this is what they called a descriptor, because the character itself is just a unique identifier. So if you visualized what the character would be, you might be able to visualize it something like this “sdfasdfa2342134987fgsdfgsdf9808fsfgsd” absolute unique character, so you can be sure that it will never replace any other piece of code in it.

What is cool about this if I create a second character like

 const person = Symbol('Bert') 

You can see that I used Burt again. Will they be the same because I described them as one and the same?

 const bert = Symbol('Bert'); const person = Symbol('Bert'); console.log(bert); console.log(person); console.log(bert === person); console.log(bert == person); 
enter image description here

This can be useful if you are creating an object of your class.

  const classRoom = { 'Mia' : { grade: 50, gender: 'female' }, 'Gilbert': { grade: 80, gender: 'male' }, 'Gilbert' { grade: 80, gender: 'male' }, }; 

But then you have another one named Gilbert, so you come across names. So imagine if you are working with millions and millions of data. Thus, instead of using the name of people or using some unique identifier, we can use a symbol for their names.

  const classRoom = { [Symbol('Mia')] : { grade: 50, gender: 'female' }, [Symbol('Gilbert')]: { grade: 80, gender: 'male' }, [Symbol('Gilbert')]: { grade: 80, gender: 'male' }, }; 

Another thing related to symbols is that they are not listed, which means that we cannot loop them if I

  for (const person in classRoom) { console.log(person); } 

I get nothing.

If you want to access all of your characters because they contain some information that you want to receive, you can use the object method.

  const syms = Object.getOwnPropertySymbols(classRoom); const data = syms.map(sym => classRoom[sym]); console.log(data); 
0
source

Source: https://habr.com/ru/post/978793/


All Articles