NB: Information from the publication in practice should be applied very carefully and under the supervision of more experienced colleagues.
for-in
loops or in the results of the Object.keys()
method, and some are visible? obj.id = 5; console.log(obj.id) // => '101' (5 в двоичной системе счисления)
const obj = { name: 'Arfat', id: 5 } obj.name // => 'Arfat'
name
and id
properties of the obj
object are called data obj
properties, or “data properties” (Data Properties). These are familiar properties that are constantly found in JavaScript code. What other kinds of properties can objects have?get
and set
.ключ: значение
, the following syntax is used: const accessorObj = { get name() { return 'Arfat'; } }; accessorObj.name; // => 'Arfat' const dataObj = { name: 'Arfat', }; dataObj.name; // => 'Arfat'
accesorObj
object and compare it with the dataObj
object. As you can see, they are now showing the same behavior. In describing the first object, we used the get
keyword, followed by the function declaration. In order to access such a property, although it is represented by a function, it is not necessary to put parentheses after the name of the property to call this function. That is a construction like accessorObj.name();
is incorrect.accessorObj.name
property, that is, when you try to read it, the corresponding function is executed and the value returned to it becomes the value of the name
property.get
functions are called getters, they are responsible for getting values. If we continue our example and try to change the value of the name
property of the accessorObj
object, say, by running the command accessorObj.name = 'New Person';
then it turns out that nothing will happen. The point here is that the setter function is not associated with the name
key. Such functions allow you to customize the order in which new values are assigned to properties of objects, access to which is organized using getters. const accessorObj = { _name: 'Arfat', get name() { return this._name; }, set name(value) { this._name = value; } };
_name
object. The first symbol of the name of such a property is an underscore, which is nothing more than a hint for the programmer, indicating that this property is intended for the internal needs of the object. Next, we work with it when accessing the property of the name
object, access to which is regulated by a getter and a setter._name
property, we can modify it. const obj = { get name() { return this._name.toUpperCase(); }, set name(value) { this._name = value; }, get id() { return this._id.toString(2); // Преобразуем десятичное число в его двоичное представление }, set id(value) { this._id = value; } } obj.name = 'Arfat'; obj.name; // => 'ARFAT' obj.id = 5; obj.id; // => '101
ключ: значение
pair.[[Value]]
[[Get]]
[[Set]]
[[Writable]]
[[Enumerable]]
[[Configurable]]
[[]]
? Double parentheses indicate that these are entities used by the internal mechanisms of the language. The JS programmer cannot access these properties directly. In order to influence them, appropriate methods are used.x
and y
. In addition, each of them is associated with a set of attributes.Object.getOwnPropertyDescriptor()
function to do this. It takes an object and the name of its property, and then returns an object containing the attributes of this property. Here is an example: const object = { x: 5, y: 6 }; Object.getOwnPropertyDescriptor(object, 'x'); /* { value: 5, writable: true, enumerable: true, configurable: true } */
[[Value]]
, [[Writable]]
, [[Enumerable]]
and [[Configurable]]
.[[Value]]
and [[Writable]]
attributes, have the [[Get]]
and [[Set]]
attributes.object.x
type, we get what is stored in the [[Value]]
attribute. The same thing will happen when trying to read the properties of an object using square brackets. const obj = { set x(val) { console.log(val) // => 23 } } obj.x = 23;
val
to the setter function. Here is the code that demonstrates the use of setters and getters.false
is stored here, then attempts to change the value of the property will not succeed.for-in
loops. If it is set to true
, then the property will be able to work using such cycles.false
:[[Enumerable]]
, [[Configurable]]
, [[Get]]
and [[Set]]
attributes will be immutable.false
also depends on the type of property. This attribute, in addition to the above effects on the properties, acts on them and so:[[Writable]]
attribute can only be changed from true
to false
.[[Writable]]
attribute is not set to false
, the [[Value]]
attribute can be changed. But after the [[Writable]]
and [[Configurable]]
attributes are set to false
, the property will be non-writable, undeletable, and immutable.undefined
, or object with a property descriptor.Object
method that allows you to add properties to objects or change existing properties. It takes three arguments — an object, a property name, and an object with a handle. This method returns a modified object. Consider an example: const obj = {}; // #1 Object.defineProperty(obj, 'id', { value: 42 }); // #2 console.log(obj); // => { } // #3 console.log(obj.id); // => 42 // #4 Object.defineProperty(obj, 'name', { value: 'Arfat', writable: false, enumerable: true, configurable: true }); // #5 console.log(obj.name); // => 'Arfat' // #6 obj.name = 'Arfat Salman' // #7 console.log(obj.name); // => 'Arfat' // (а не 'Arfat Salman') Object.defineProperty(obj, 'lastName', { value: 'Salman', enumerable: false, }); console.log(Object.keys(obj)); // => [ 'name' ] // #8 delete obj.id; // #9 console.log(obj.id); // => 42 //#10 Object.defineProperties(obj, { property1: { value: 42, writable: true }, property2: {} }); console.log(obj.property1) // => 42
// #n
.#1
we use the defineProperty
function, passing it an obj
object, the name of the id
property and a descriptor object that contains only the value
property, indicating that the value [ 42
[[Value]]
will be written to the [[Value]]
attribute. Remember that if you do not pass values for attributes like [[Enumerable]]
or [[Configurable]]
in this object, they will be set to false
by default. In this case, the [[Writable]]
, [[Enumerable]]
and [[Configurable]]
attributes of id
set to false
.#2
, we are trying to output a string representation of the object to the console. Since its id
property is not enumerable, it will not be displayed. At the same time, the property exists, which proves its successful output by command #3
.#4
), we set the complete list of attributes. In particular, we set [[Writable]]
to false
.#5
and #7
we display the value of the name
property. But between them (fragment #6
) we tried to change this value. This operation did not change the value of a property, because its [[Writable]]
attribute is set to false
. As a result, both commands output the same to the console.#8
is an attempt to delete the id
property. Recall that its [[Configurable]]
attribute is set to false
, which means that you cannot delete it. This is proved by team #9
.#10
shows the use of the Object.defineProperties () function. It works in the same way as the defineProperty()
function, but allows, in one call, to affect several properties of an object, while defineProperty()
works with only one property of an object.Object.preventExtensions()
method prevents the object from expanding, that is, adding new properties to it. It takes an object and makes it non-expandable. Note that you can delete properties from such an object. Consider an example: const obj = { id: 42 }; Object.preventExtensions(obj); obj.name = 'Arfat'; console.log(obj); // => { id: 42 }
Object.isExtensible()
method. If it returns true
, then new properties can be added to the object.seal()
method “seals” objects. This is what it is about:Object.preventExtensions()
).[[Writable]]
attribute is not set to false
, can be changed. const obj = { id: 42 }; Object.seal(obj); delete obj.id // (не работает) obj.name = 'Arfat'; // (не работает) console.log(obj); // => { id: 42 } Object.isExtensible(obj); // => false Object.isSealed(obj); //=> true
Object.isSealed()
method.freeze()
method allows you to “freeze” objects, equipping them with the maximum possible JavaScript level of protection. Here is how it works:Object.seal()
. const obj = { id: 42 }; Object.freeze(obj); delete obj.id // (не работает) obj.name = 'Arfat'; // (не работает) console.log(obj); // => { id: 42 } Object.isExtensible(obj); // => false Object.isSealed(obj); //=> true Object.isFrozen(obj); // => true
Object.isFrozen()
method.Property creation | Reading property | Property rewrite | Property removal | |
Object.freeze() | - | + | - | - |
Object.seal() | - | + | + | - |
Object.preventExtensions() | - | + | + | + |
Source: https://habr.com/ru/post/438794/