Javascript Classes Flashcards

1
Q

What is a class expression?

A

A class expression is when a class declaration (named or unnamed) is assigned to a variable.

You can access the classes name using the name property.

// unnamed
let Rectangle = class {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
};
console.log(Rectangle.name);
// output: "Rectangle"
// named
let Rectangle = class Rectangle2 {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
};
console.log(Rectangle.name);
// output: "Rectangle2"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are classes?

A

Classes are a template for creating objects. They encapsulate data with code that works on that data.

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

What is a class declaration and how would you create one?

A

This is the standard way a class is created in most programming languages. To create a class declaration you use the class keyword.

class Rectangle {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Does a class need to be defined before it can be used?

A

Yes. Class declarations and expressions do not get hoisted.

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

How do you instantiate an object?

A

You use the new keyword.

myRectangle = new Rectangle(300, 500);
console.log(myRectangle);
Output:
{
    "height": 300,
    "width": 500
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How can you view a classes definition?

A

Just console.log() the classname.

console.log(Rectangle)

Output: 
class Rectangle {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a class constructor method?

A

The constructor method is a special method for creating and initialising an object created with a class. In the example below the constructor is used to set the values of the height and width properties of the object ‘myRectangle’ when it is instantiated.

let Rectangle = class {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
};

myRectangle = new Rectangle(300, 500);

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

Can you have more than one constructor method in a class declaration ?

A

No you can only have one constructor method. More than one will throw an error.

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

How do you declare a private property in a class?

A

You precede the property name with a hashtag.

class Rectangle {
  #height = 0;
  #width;
  constructor(height, width) {
    this.#height = height;
    this.#width = width;
  }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a super class?

A

A super class is the class that is the parent of your new class.

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

In the following code what is the super class and what is the sub class ?

class Animal {
  constructor(name) {
    this.name = name;
  }

speak() {
console.log(${this.name} makes a noise.);
}
}

class Dog extends Animal {
  constructor(name) {
    super(name); // call the super class constructor and pass in the name parameter
  }

speak() {
console.log(${this.name} barks.);
}
}

let d = new Dog('Mitzie');
d.speak(); // Mitzie barks.
A

The super class is Animal, this is because Dog extends from Animal. The sub class is Dog, this is because Dog is extending from the super class Animal.

Parent - Super class
Child - Sub class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

If a super class has a constructor what must the sub class do in its constructor?

A

The sub class must call the super class’s constructor. This is done using the super class constructor.

class Animal {
  constructor(name) {
    this.name = name;
  }

speak() {
console.log(${this.name} makes a noise.);
}
}

class Dog extends Animal {
  constructor(name) {
    super(name); // call the super class constructor and pass in the name parameter
  }

speak() {
console.log(${this.name} barks.);
}
}

let d = new Dog('Mitzie');
d.speak(); // Mitzie barks.
  speak() {
    console.log(`${this.name} barks.`);
  }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you create a method in a class?

A

Within the class body you state the method name parenthesis and function body.

class Rectangle {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
  // Method
  calcArea() {
    return this.height * this.width;
  }
}

const square = new Rectangle(10, 10);

console.log(square.calcArea() ); // 100

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

How do you create a getter in a class and how do you access it?

A

You create it just like a method but add the ‘get’ keyword in front of the method. When you call the getter you call it without parenthesis as if it is an object property.

class Rectangle {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
  // Getter
  get calcArea() {
    return this.height * this.width;
  }
}

const square = new Rectangle(10, 10);

console.log(square.calcArea ); // No parenthesis

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