Classes Flashcards

1
Q

Accessor properties are represented by what methods?

A

getter & setter

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

In an object literal, getter and setter are denoted by?

A

get & set

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

What does getter do?

A

Getter binds an object property to a function which is called when it’s looked up

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

What does setter do?

A

Setter bind an object property to a function which is called when someone tries to set it

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

What is the basic “class” syntax?

A

class MyClass {
constructor() {…};
method1() {…};
method2() {…};
}

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

What do you need to use in order to create a new object in class with all the listed methods?

A

new MyClass()

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

What happens inside “class” if you call the ‘new’

A

constructor() is automatically called inside “class” which means we can initialize the object there

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

What do you put between class methods?

A

Nothing!

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

Where does “class” store its methods? (e.g class MyClass())

A

In MyClass.prototype

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

What type is class? More precisely?

A

Function, more precisely the constructor method

// class is a function
alert(typeof User); // function

// …or, more precisely, the constructor method
alert(User === User.prototype.constructor); // true

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

What are the three differences between classes and constructors?

A
  1. Function created by class is labelled by a special internal property [[IsClassConstructor]]
  2. Class methods are non-enumerable
  3. Classes always “use strict”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a “class field”?

A

Syntax that allows you to add any property

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

What is the difference between a “class field” and methods inside class? (e.g class User())

A

Class field are set on individual objects, not on User.prototype

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

What are the two ways to bind inside class functions? (syntax)

A
  1. Pass a wrapper-function such as
    setTimeout(function() {
    user.sayHi(); // Hello, John!
    }, 1000);
  2. The class field “click” is created on a per-object basis, there’s a separate function for each Button object, with this inside it referencing that object. We can pass button.click around anywhere, and the value of this will always be correct.

class Button {
constructor(value) {
this.value = value;
}
click = () => {
alert(this.value);
}
}

let button = new Button(“hello”);

setTimeout(button.click, 1000); // hello

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

What is an important difference between class declaration and function declaration?

A

To call function, it doesn’t matter whether the function has been written before or after but classes MUST be defined before they are constructed

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

What are the cons of classes in JavaScript?

A
  1. Bad for functional programming (which is using functions to their fullest extent). There’s a principle “composition over inheritance” but with classes there is “inheritance over composition”
  2. Conceptually there are no classes in JS and they are not needed there