Objects, Prototypes and Classes Flashcards

1
Q

What is a property shorthand syntax?

A

In ES6, the shorthand syntax has been introduced. When creating new objects, it allows to get rid off the duplication.

As in: 
function registerUser(firstName, lastName) {
let person = {
firstName,
lastName
};
display(person);
}

This function creates a person object and takes firstName property to a variable that is also named firstName.

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

What does Object.keys( ) function do?

A

Helps discover all of the properties and methods on an object.
It takes an existing object like person as a parameter and returns an array of the names of that objects properties and methods.

Object.keys(person)

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

How can you loop through properties in objects?

A

for … in
as for (let propertyName in person) {
push.arr(propertyName)
}

This for loop will loop over all object’s properties and methods and each time will set propertyName variable to the name of each property.

Object.keys and for in accomplish the same thing - give access to each of the properties and methods names on an object.

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

List JS Equality Operators and their purpose when it comes to objects.

A

== - Useful in rare cases, should be avoided, because it’s not type-safe.

=== - most common, type-safe, Nan not equal to NaN,
0+ = 0-

Object.is() - almost identical to ===, except for a few mathematical differences.
NaN equals Nan
+0 does not equal -0

Syntax of Object.is(person1,person2)

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

What it means for two objects to be equal?

A

If we create a person Object it gets stored in a memory address.
When we create another person Object it will be stored in another memory person address.
What actually is compared are not the values within an object, but the actual memory address of these objects. It means that using JS equality operators the result of comparison will always be false…
Unless we store the object in the same memory address by let person2 = person1

If we compare primitive types, all of the equality operators will consider them equal.

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

What Object.assign() function does?

A

It helps to create a new object from an existing one, or merge properties of two objects.

Object.assign(person2, person1)
It takes multiple parameters: the first one is the object that is going to be modified, properties from second parameter will be merged into the first one. Object.assign MUTATES the object that was passed in as the first parameter and returns it.

To achieve object immutability in Object.assign() function we can pass another empty object as its first parameter like:
Object.assign({}, person, healthStats)

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

What do good immutability practices say?

A

A function shouldn’t really mutate objects that are passed to them.

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

Explain this and new keyword concepts.

A

In Object programming.

this. keyword always refers to an object. Which object it refers to depends on the context of the code you are executing.

In let person = new Person();

this. keyword referred to a new empty object.
new creates a new Javascript object, and sets the context of this. to that new object and then calls constructor function.

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

Using bracket notation for properties.

A

You can access object properties using bracket notations just as dot notation.

for instance person[‘firstName’]

This is especially useful in conjuction with Object.keys() or for … in loop to display all property values on an object.

for (let propertyName in person){
arr.push(person[propertyName])
}

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

What are property descriptors of objects

A

Property descriptors present in objects come in two main flavors: data descriptors and accessor descriptors.

writable attribute defines whether the properties value can be changed from its initial value. To change it:
Object.defineProperty(person, ‘firstName’, {writable: false})

enumerable - by default properties on an object are enumerable. Meaning we can enumerate over them with for … in loops and list them with Object.keys() function. Also JSON serialize will not list this {enumerable: false} property

configurable - this attribute prevents the property descriptors from being changed. Also prevents property to be delete from an object. (delete person.firstName).

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

What are getters and setters?

A

These two keywords define accessor functions: a getter and a setter for the fullName property. When the property is accessed, the return value from the getter is used. When a value is set, the setter is called and passed the value that was set.

Object.defineProperty(person, 'fullName',
{
  get: function () {
    return this.name.first + ' ' + this.name.last;
  },
  set: function(value) {
    let nameParts = value.split(' ');
    this.name.first = nameParts[0];
    this.name.last = nameParts[1];
  }
});
person.fullName = 'Fred Jones';
person.name.first
person.name.last
will be set to Fred and Jones.

With classes syntax, the implementation is much quicker and cleaner.

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

What is the difference between a function’s prototype and objects prototype?

A

A function’s prototype is the object instance that will become the prototype for all objects created using this function as a constructor.

An Object’s Prototype is the object instance from which the object is inherited.

You can access object’s prototypes by either Person.prototype or obj.__proto__

__proto__ is the actual object that is used in the lookup chain to resolve methods, etc. prototype is the object that is used to build __proto__ when you create an object with new:

When we compare Person.prototype === obj.__proto__ they are equal.

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

What is the role of super() keyword?

A
This keyword is used to call the constructor on the class that we are extending.
As in:
class Student extends Person {
  constructor(firstName, lastName, age) {
    super(firstName,lastName,age);
    this._enrolledCourses = [];
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the convention of naming properties that are modified by getters and setters?

A

We use _ to create a separate value in which to store our name property. Without using this convention, we would get errors every time we called get or set.

class Teacher extends Person {
  constructor(first, last, age, gender, interests, subject, grade) {
    super(first, last, age, gender, interests);
    // subject and grade are specific to Teacher
    this._subject = subject;
    this.grade = grade;
  }
  get subject() {
    return this._subject;
  }
  set subject(newSubject) {
    this._subject = newSubject;
  }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a prototype?

A

Prototype is a template object for primitives and other objects. It typically, contains a bunch of methods, that the instantiated object will have access to.
Then we can create multiple objects that share the same prototype.

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

What is a constructor function inside of a class?

A

Constructor function is a function that gets called immediately whenever we create a new instance of a class.