JS objects Flashcards

1
Q

What is an object in JS?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain: objects are mutable and manipulated by reference rather than by value?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Name 5 property attributes:

A
  1. Name -
  2. Value -
  3. Writable - this attribute specifies whether the value of the property can be set.
  4. Enumarable - specifies wheter the property name os returned by a for/ in loop.
  5. Configurable - specifies whether the property can be deleted and whether its attributes can be altered.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How can we create Object in JS?

A
  1. {} - object literal;
  2. Object.create();
  3. with the help of new keywrod.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can we create an object with new keyword?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do we create object with Object.create()?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to create simple object with Object.create()

A

You need to pass Object.prototype as argument.

let object3 - Object.create(Object.prototype)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to obtain and set a property?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to delete property from the object?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How in operator works for testing properties in Objects?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How hasOwnProperty() method works in Objects?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How propertyIsEnumerable() method works in Objects?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How propertyIsEnumerable() method works in Objects?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How can we test properties instead of using in operator?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the difference between using in and !== undefined?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How Object.keys() works?

A

Object.keys() returns an array of the names of the enumerable own properties of an object. It does not include non-enumerable properties, inherited properties, or properties whose name is a Symbol

let o = {x: 1, y: 2};
Object.keys(o) // ['x', 'y'];
17
Q

Write code that will combine to objects;
Use ES5
let target = {x: 1};
let source = {y: 2, z: 3};

A
let target = {x: 1};
let source = {y: 2, z: 3};
for (let key of Object.create(source)) {
   target[key] = source[key]
}
18
Q

How Object.assign() works?

A

Object.assign() expects two or more objects as its arguments. It modifies and returns the first argument, which is the target object, but does not alter the second or any subsequent arguments, which are the source objects. For each source object, it copies the enumerable own properties of that object (including those whose names are Symbols) into the target object. It processes the source objects in argument list order so that properties in the first source object override properties by the same name in the target object and properties in the second source object (if there is one) override properties with the same name in the first source object.

One reason to assign properties from one object into another is when you have an object that defines default values for many properties and you want to copy those default properties into another object if a property by that name does not already exist in that object. Using Object.assign() naively will not do what you want:

Object.assign(o, defaults); // overwrites everything in o with defaults

Instead, what you can do is to create a new object, copy the defaults into it, and then
override those defaults with the properties in o:
o = Object.assign({}, defaults, o);

19
Q

How we can create a shallow copy of an object?

A

Using the Spread Operator (…):
The spread operator allows you to create a shallow copy of an object by spreading its properties into a new object.

const originalObj = { key: 'value' };
const copiedObj = { ...originalObj };

console.log(copiedObj); // { key: 'value' }

Using Object.assign():
The Object.assign() method can be used to copy the properties from one or more source objects into a target object.

const originalObj = { key: 'value' };
const copiedObj = Object.assign({}, originalObj);

console.log(copiedObj); // { key: 'value' }
20
Q

How we can create a deep clone of a object?

A

We can use lodash .cloneDeep();
Or write a function using recursion.