JavaScript Objects Flashcards

1
Q

How do you create a JS object?

A
let user = new Object(); // "object constructor" syntax
let user = {};  // "object literal" syntax
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you remove an attribute from an object?

A

With “delete”

delete user.age;

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

Can you use mutliword property names for objects?

A

Yes, but they must be quoted:

let user = {
  name: "John",
  age: 30,
  "likes birds": true  // multiword property name must be quoted
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you access multiword properties?

A

With square notation:

user[“likes birds”]

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

What is the result of the following code?

let fruit = ‘Apple’

let bag = {
  [fruit]: 5,
};

alert( bag.apple );

A

5

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

What is the value of “bag”?

let fruit = 'apple';
let bag = {
  [fruit + 'Computers']: 5
};
A

{
appleComputers: 5
}

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

What is printed?

let obj = {
  for: 1,
  let: 2,
  return: 3
};

alert( obj.for + obj.let + obj.return );

A

6

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

What values can be used as object property names?

A

Basically, any name is allowed, but there’s a special one: “__proto__” that gets special treatment for historical reasons.

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

What is the result of the following?

let name=0;
let age = 0;
return {
    name,
    age
  };
A

{
name: 0,
age: 0
}

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

How can you verify if a property exist in an object?

A

With the “in” operator.

“key” in obj // true if it exists

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

What will the second alert print?

let obj = {
  test: undefined
};
alert( obj.test ); // it's undefined, so - no such property?
alert( "test" in obj );
A

true

The property obj.test technically exists. So the in operator works right.

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

How do you iterate through all the keys in an object using a “for” statement

A

With “for .. in”

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

Are the properties in an object ordered when accessing them with a “for .. in” loop?

A

Yes, they are “ordered in a special fashion”: integer properties are sorted, the all others appear in creation order.

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

What will the alert print?

let user = { name: 'John' };
let admin = user;
admin.name = 'Pete'; 
alert(user.name);
A

‘Pete’,

the property was updated by the “admin” reference but changes are seen from the “user” reference and both point to the same object

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

What will the two alerts print?

let a = {};
let b = a; // copy the reference

alert( a == b );
alert( a === b );

A

true and true

both variables reference the same object

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

When comparing two objects, what is the difference between == and ===?

A

Nothing, both work exactly the same

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

What will happen with the following constant-declared object?

const user = {
  name: "John"
};

user.age = 25;
alert(user.age);

A

The alert will print 25.

Constant-defined objects can change their property values. What cannot change is the value of “user” itself

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

What will happen with the following constant-declared object?

const user = {
  name: "John"
};
// Error (can't reassign user)
user = {
  name: "Pete"
};
A

An error will be thrown. Can’t reassign constant variables

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

How can you create the copy of an object?

A

By copying each property, one by one, to a new object, or by using Object.assign().

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

What does “deep clone” an object mean?

A

Copying an object, making sure that if one or many properties are objects themselves, those objects are also cloned

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

What does “Object.assign”

A

It takes multiple objects as parameters and returns a new object which contains all the cloned properties of the parameters

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

How is memory managed (allocated and freed) in JavaScript

A

Memory is allocated in the heap and it’s freed by the Garbage Collector

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

When is memory freed in JavaScript?

A

When objects are not reachable, they are marked by the Garbage Collector for deletion.

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

When is an object considered “unreachable”?

A

When there are no variables with references pointing tot he object

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

What is an internal algorithm used by the Garbage Collector in JavaScript?

A

It’s mark-and-sweep

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

How does “mark-and-sweep” word?

A

The garbage collector takes roots and “marks” (remembers) them.
Then it visits and “marks” all references from them.
Then it visits marked objects and marks their references. All visited objects are remembered, so as not to visit the same object twice in the future.
…And so on until every reachable (from the roots) references are visited.
All objects except marked ones are removed.

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

What are some optimizations done for the Garbage Collector to not affect execution?

A

Generational collection, incremental collection and idle-time collection

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

What is “general collection” in the Garbage Collector?

A

Objects are split into two sets: “new ones” and “old ones”. Many objects appear, do their job and die fast, they can be cleaned up aggressively. Those that survive for long enough, become “old” and are examined less often.

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

What is “incremental collection” in the Garbage Collector?

A

If there are many objects, and we try to walk and mark the whole object set at once, it may take some time and introduce visible delays in the execution. So the engine tries to split the garbage collection into pieces. Then the pieces are executed one by one, separately. That requires some extra bookkeeping between them to track changes, but we have many tiny delays instead of a big one.

30
Q

What is “idle-time” in the Garbage Collector?

A

The garbage collector tries to run only while the CPU is idle, to reduce the possible effect on the execution.

31
Q

How can you force running the Garbage Collector

A

It cannot be forced

32
Q

How can you prevent the Garbage Collector from running?

A

It cannot be prevented

33
Q

What are Symbols?

A

A “symbol” represents a unique identifier. A value of this type can be created using Symbol()

34
Q

How can you add a description to a Symbol and what is that description for?

A

The description can be passed as a string to the constructor:

let id = Symbol(“id”);

It’s mostly used for debugging purposes

35
Q

What will the alert print?

let id1 = Symbol("id");
let id2 = Symbol("id");

alert(id1 == id2);

A

false

Even if they have the same description, both symbols are different

36
Q

What will the alert print?

let id = Symbol("id");
alert(id);
A

An error will be thrown

TypeError: Cannot convert a Symbol value to a string

37
Q

How can you convert a Symbol into a String?

A

Using “toString”

let id = Symbol("id");
alert(id.toString()); // Symbol(id), now it works

Or get symbol.description property to show the description only

38
Q

What are Symbols used for?

A

Symbols allow us to create “hidden” properties of an object, that no other part of code can accidentally access or overwrite.

For instance, if we’re working with user objects, that belong to a third-party code.

39
Q

What’s the benefit of using Symbol(“id”) over a string “id”?

A

A symbol cannot be accessed or overriden accidentally: If third-party tries to set Symbol(“id”), there will be no conflict between our and their identifiers, because symbols are always different, even if they have the same name.

40
Q

Are Symbols included in “for .. in” loops?

A

No, symbolic properties do not participate in for..in loop.

41
Q

Are Symbols included in Object.keys(user)?

A

No, symbolic properties are not included

42
Q

Are Symbols copied with Object.assign?

A

Yes, Object.assign copies both string and symbol properties:

43
Q

What will the following alerts print?

let obj = {
  0: "test" // same as "0": "test"
};

// both alerts access the same property (the number 0 is converted to string “0”)
alert( obj[“0”] ); // test
alert( obj[0] ); // test (same property)

A

Both print “test”.

We can only use strings or symbols as keys in objects. Other types are converted to strings.

For instance, a number 0 becomes a string “0” when used as a property key:

44
Q

Can you retrieve an existing Symbol?

A

Yes, you can retrieve it from the Symbol Registry

45
Q

What does Symbol.for(key) do?

A

In order to read (create if absent) a symbol from the registry, use Symbol.for(key).

46
Q

What does Symbol.keyFor do?

A

The Symbol.keyFor internally uses the global symbol registry to look up the key for the symbol.

47
Q

What will Symbol.keyFor return if the parameter is not a symbol or the symbol is not global?

A

Symbol.keyFor will return undefined.

48
Q

How can you retrieve the description of a Symbol

A

With the “description” property

49
Q

Mention 4 well known symbols

A

Symbol.hasInstance
Symbol.isConcatSpreadable
Symbol.iterator
Symbol.toPrimitive

50
Q

What is a method?

A

A function that is the property of an object is called its method.

51
Q

What will the alert print?

let user = {

name: “John”,
age: 30,

  sayHi() {
    alert(this.name);
  }
};
user.sayHi();
A

“John”

“this” points to user

52
Q

What will the alert print?

let user = {

name: “John”,
age: 30,

sayHi() {
alert(user.name);
}

};

A

“John”

user.name is the same as this.name, inside “sayHi”

53
Q

What will the alert print?

let user = {

name: “John”,
age: 30,

sayHi() {
alert( user.name );
}

};

let admin = user;
user = null;
admin.sayHi();
A

An error is thrown as “user” now points to null.

54
Q

When is “this” evaluated?

A

The value for “this” is evaluated during run-time

55
Q

Can a function have different values for “this”?

A

The value of this is evaluated during the run-time, depending on the context.

If the same function is assigned to two different objects it has different “this” values in the calls.

56
Q

What will the alert print in strict mode?

function sayHi() {
alert(this);
}
sayHi();

A

undefined

57
Q

What will the alert print in non-strict mode?

function sayHi() {
alert(this);
}
sayHi();

A

the global object (window in a browser)

58
Q

What will the alert print?

let user = {
  name: "John",
  hi() { alert(this.name); },
  bye() { alert("Bye"); }
};

(user.name == “John” ? user.hi : user.bye)();

A

An error is thrown.

The value of “this” inside the call becomes undefined.

59
Q

What is the difference between in values for “this” in

let hi = user.hi;
hi(); 

and

user.hi()

A

For the first one, “this” inside hi() is undefined. The second “this” will point to user.

60
Q

Why is “this” undefined in

let hi = user.hi;
hi(); 

but not in

user.hi()

A

To make user.hi() calls work, JavaScript uses a trick – the dot ‘.’ returns not a function, but a value of the special Reference Type.

When parentheses () are called on the Reference Type, they receive the full information about the object and its method, and can set the right this (=user in this case).

61
Q

What is the value of “this” for arrow functions?

A

Arrow functions are special: they don’t have their “own” this. If we reference this from such a function, it’s taken from the outer “normal” function.

62
Q

How can objects be converted to primitives?

A

Yes, by implementing ob[Symbol.toPrimitive]:

obj[Symbol.toPrimitive] = function(hint) {
  // must return a primitive value
};
63
Q

For the “Symbol.toPrimitive” function, what are valid values for the hints?

A

one of “string”, “number”, “default”

64
Q

For the “Symbol.toPrimitive” function, what does the “default” hint indicates?

A

For cases when the operator is “not sure” what type to expect.

For instance, binary plus + can work both with strings (concatenates them) and numbers (adds them), so both strings and numbers would do. So if the a binary plus gets an object as an argument, it uses the “default” hint to convert it.

65
Q

What is the difference between “toPrimitive” and “toString”

A

If “toPrimitive” is not implemented:

if hint is “string” try obj.toString() and obj.valueOf(), whatever exists.
Otherwise if hint is “number” or “default” try obj.valueOf() and obj.toString(), whatever exists.

66
Q

What happens if toString or valueOf returns an object?

A

It’s ignored (same as if there were no method).

67
Q

If not implemented for an object, what does “toString” return?

A

“[object Object]”

68
Q

If not implemented for an object, what does “valueOf” return?

A

The object itself

69
Q

Does the “Symbol.toPrimitive” function have to return the hinted type?

A

No, it doesn’t

70
Q

What data types can be returned from “Symbol.toPrimitive”?

A

Anything, as long as its a primitive

71
Q

What happens if you try to return an object from “Symbol.toPrimitive”?

A

An error is thrown

72
Q

What are the requirements for object constructors?

A

They are named with capital letter first.

They should be executed only with “new” operator.