JS objects Flashcards
What is an object in JS?
An object is an unordered collection of properties, each of which has a name and a value. Propertie of object can have any type, names are usually strings but also can be a symblo.
Explain: objects are mutable and manipulated by reference rather than by value?
If the variable x referes to an object and the code let x = y is executed, the variable y holds a reference to the same object, not a copy of that object. Any modifications made to the object through the variable y are visible through the variable x.
Name 5 property attributes:
- Name -
- Value -
- Writable - this attribute specifies whether the value of the property can be set.
- Enumarable - specifies wheter the property name os returned by a for/ in loop.
- Configurable - specifies whether the property can be deleted and whether its attributes can be altered.
How can we create Object in JS?
- {} - object literal;
- Object.create();
- with the help of new keywrod.
How can we create an object with new keyword?
let objectX = new Object();
The new keyword creates and initializes a new object. The new keywrod must be followed by a function invocation. A function used in this way is called a constructor and serves to initialize a newly created object.
How do we create object with Object.create()?
let objectX = Object.create({x: 1, y: 2})
Object.create() creates a new object, using its first argument as the prototype of that object. You can even pass null let x = Object.create(null)
- in this case we will have object without any methods.
How to create simple object with Object.create()
You need to pass Object.prototype as argument.
let object3 - Object.create(Object.prototype)
How to obtain and set a property?
We can use the dot (.) or square bracket operators:
let author = book.author; let title = book['main title'];
To set:
book.edition = 7;
book[‘main title’] = ‘Ecma Script’;
How to delete property from the object?
We can use delete operator.delete book.author
Delete operator only deletes own properties, not inherited ones.
A delete exepression evaluated in true (if the delete succeeded) or false .
How in operator works for testing properties in Objects?
The in operator expects a property name on its left side and an object on its right. It returns true if the object has an own property or an inherited property by that name:
let o = {x: 1}; "x" in o // => true: o has an own property "x" "y" in o // => false: o doesn't have a property "y" "toString" in o // => true: o inherits a toString property
How hasOwnProperty() method works in Objects?
The hasOwnProperty() method of an object tests whether that object has an own property with the given name. It returns false for inherited properties:
let o = {x: 1}; o.hasOwnProperty("x") // => true: o has an own property x o.hasOwnProperty("y") // => false: o doesn't have a property y o.hasOwnProperty("toString") // => false: toString is an inherited property
How propertyIsEnumerable() method works in Objects?
It returns true only if the named property is an own property and its enumerable attribute is true. Certain built-in properties are not enumerable.
let o = {x: 1}; o.propertyIsEnumerable("x") // => true: o has an own enumerable property x
How propertyIsEnumerable() method works in Objects?
It returns true only if the named property is an own property and its enumerable attribute is true. Certain built-in properties are not enumerable.
let o = {x: 1}; o.propertyIsEnumerable("x") // => true: o has an own enumerable property x
How can we test properties instead of using in operator?
Instead of using the in operator, it is often sufficient to simply query the property and use !== to make sure it is not undefined:
let o = {x: 1}; o.x !== undefined // => true: o has a property x o.y !== undefined // => false: o doesn't have a property y o.toString !== undefined // => true: o inherits a toString property
What is the difference between using in and !== undefined?
There is one thing the in operator can do that the simple property access technique shown here cannot do. in can distinguish between properties that do not exist and properties that exist but have been set to undefined.
let o = { x: undefined }; // Property is explicitly set to undefined o.x !== undefined o.y !== undefined "x"ino "y"ino delete o.x; "x"ino // => false: property exists but is undefined // => false: property doesn't even exist // => true: the property exists // => false: the property doesn't exist // Delete the property x // => false: it doesn't exist anymore