Object
been extended with one method that allows you to get all the symbols of an object: var role = Symbol('role'); var score = Symbol('score'); var id = 100; var name = 'Moderator'; var user = { id: id, name: name }; user[role] = 'admin'; user[score] = 50000; Object.getOwnPropertySymbols( user ); // [Symbol(role), Symbol(score)]
var userSymbols = Object.getOwnPropertySymbols( user ); var roleSymbol = userSymbols[0]; user[ roleSymbol ]; // 'admin'
Object.getOwnPropertyNames
.Reflect
object which allows reflection (or reflection) in JavaScript. And one of the methods of the Reflect
object allows you to get all the properties that are declared through both strings and characters. var properties = Reflect.ownKeys( user ); console.log( user ); // ['id', 'name', Symbol(role), Symbol(score)]
var Project = (function () { var projectData = Symbol('project'); var projectStatus = Symbol('status'); var _getTitle = Symbol('title'); function Project( data, status ) { this[ projectData ] = data; this[ projectStatus ] = status; } Project.prototype.getProjectTitle = function () { return this[ _getTitle ](); }; Project.prototype[ _getTitle ] = function () { return this[ projectData ].name + ' (' + this[ projectData ].description + ')'; }; Project.prototype.getStatus = function () { return this[ projectStatus ]; }; Project.prototype.changeStatus = function ( status ) { this[ projectStatus ] = status; }; return Project; }()); var project = new Project({ name: 'Application', description: 'favorite project' }, 'open'); console.log( project.getStatus() ); // 'open' project.changeStatus('finished'); console.log( project.getStatus() ); // 'finished' console.log( project.getProjectTitle() ); // 'Application (favorite project)'
_getTitle
method to which you can only refer to a symbol, which hides it from the public API and is used only when calling getProjectTitle
. Symbols also allowed us to bring all the methods to the prototype object. var Request = (function () { var requestState = Symbol('state'); var states = { NOT_INITIALIZED: Symbol(), RECEIVED: Symbol(), PROCESSING: Symbol(), FINISHED: Symbol() }; function Request() { this[ requestState ] = states.NOT_INITIALIZED; } Request.prototype.getStates = function () { return states; }; Request.prototype.close = function () { this[ requestState ] = states.FINISHED; }; Request.prototype.changeState = function ( state ) { this[ requestState ] = state; }; return Request; }()) var request = new Request(); var handledState = Symbol('state'); request[ handledState ] = false; // code request[ handledState ] = true; request.close();
Request
entity provides. This module defines the requestState
state required for the module to work using the symbol. The developer also enters his state (for example, the state that displays or the request is processed), also using the symbol. This way of declaring properties through symbols gives us a guarantee that the behavior of the module will not be redefined.states
object can be treated as an enumerated type with unique values (which allows you to check or pass the correct value).RegExp
, Array
, Date
, etc. And the problem is that the characters also exist and are unique only within the context in which they are declared. If your code interacts with the code in the iframe, then in this iframe you will not have references to symbols and you will not be able to access the properties that will be declared through these symbols.Symbol.for
method: var UUIDSymbol = Symbol.for('uuid');
vm
module that allows you to execute code in a different context. var vm = require('vm'); var ourArray = Array; var ourSymbol = Symbol('uuid'); var theirArray = vm.runInNewContext('Array'); var theirSymbol = vm.runInNewContext('Symbol("uuid")'); ourArray === theirArray; // false ourSymbol === theirSymbol; // false
Array
in the current context is not equal to Array
in a different context (each has its own copy). Similar behavior as in the situation with iframe and with symbols, they are not equal to each other in different contexts. var vm = require('vm'); var ourSymbol = Symbol.for('uuid'); var theirSymbol = vm.runInNewContext('Symbol.for("uuid")'); ourSymbol === theirSymbol; // true
Symbol.for
declaring symbols through Symbol.for
we are able to use them in all contexts. If your project uses a vm
module with code execution with specific contexts, using Symbol.for
would be an excellent solution in this situation.Symbol.keyFor
: var UUIDSymbolKey = Symbol.keyFor( UUIDSymbol );
undefined
.Symbol('score')
- will not create a symbol in the global registry.Symbol
object, as well as touch on such a thing as Well-known symbols and see what opportunities they open up for us.Source: https://habr.com/ru/post/255389/
All Articles