enumerable: false
for a property, which allows you to hide a property from being listed in for-in
and will not be visible in Object.keys
, but for this you need to declare it via the Object.defineProperty
construction. var user = {}; Object.defineProperty( user, 'role', { enumerable: false, value: 'admin' });
var userRole = user.role; // 'admin'
var role = Symbol(); var user = { id: 1001, name: 'Administrator', [role]: 'admin' };
user
object is declared, in which two properties are declared via string identifiers ( id
, name
) and one property via a symbol ( role
).role
property is declared in square brackets so that it is not interpreted as a string, but was obtained by evaluating the expression. This object can also be declared as follows in order to better understand this construction: var role = Symbol(); var user = { ['id']: 1001, ['name']: 'Administrator', [role]: 'admin' };
user
object needs to get the user name, you need to write this code: var userName = user.name; // 'Administrator' // OR var userName = user['name']; // 'Administrator'
var userRole = user.role; // undefined // OR var userRole = user['role']; // undefined
var role = Symbol(); var user = { id: 1001, name: 'Administrator', [role]: 'admin' }; var userRole = user[role]; // 'admin'
for-in
, Object.keys
, Object.getOwnPropertyNames
, and will not be added when using JSON.stringify
.Symbol
function: var score = Symbol();
Symbol
function also takes an optional parameter — a string that serves to describe a symbol: var score = Symbol('user score'); console.log( score ); // Symbol(user score)
var score = new Symbol('score'); // TypeError
String
, Number
and Boolean
, the error will not be thrown away (but it is better not to use the default behavior). If you want to work not with the primitive, but with its object, you can use the Object
function passing it to the primitive as a parameter: var symbol = Symbol('symbol'); var string = 'string'; var number = 5; var symbolObj = Object( symbol ); var stringObj = Object( string ); var numberObj = Object( number ); console.log( symbol ); // Symbol(symbol) console.log( string ); // 'string' console.log( number ); // 5 console.log( symbolObj ); // Symbol {} console.log( stringObj ); // String { 0: 's', 1: 't', 2: 'r', 3: 'i', 4: 'n', 5: 'g', length: 6, [[PrimitiveValue]]: 'string' } console.log( numberObj ); // Number { [[PrimitiveValue]]: 5 }
var firstScore = Symbol('score'); var secondScore = Symbol('score'); firstScore === secondScore; // false
symbol
is returned: function isSymbol( value ) { return typeof value === 'symbol'; } var firstScore = Symbol('score'); var secondScore = 'score'; isSymbol( firstScore ); // true isSymbol( secondScore ); // false
TypeError
error will be thrown. This behavior is chosen in order not to accidentally create a string value, which will eventually be used as the property name: var userObject = {}; var role = Symbol() + 'type'; var id = 10001; userObject.id = id; userObject[ role ] = 'admin';
role
variable, if the string is, then the userObject[ role ] = 'admin'
property will be declared through the string and it will be directly accessed (but because the symbol was used, most likely there was a desire hide property value). On the other hand, if the expression results in a symbol, and since you cannot get the values of a symbol, it means you cannot determine if the type
string is in it, and this is no longer an obvious behavior and you need to inform the developer in situations when he intentionally tries to create a string value from the symbol , because such a construction does not make sense.Object.getOwnPropertySymbols
works), and also look at possible examples of using the symbol.Source: https://habr.com/ru/post/255137/
All Articles