0
0
Javascriptprogramming~20 mins

Why classes are introduced in Javascript - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Why Classes Are Introduced
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use classes instead of functions?

In JavaScript, why were classes introduced when we already had functions?

ATo replace all functions with a new syntax that does not support inheritance.
BTo provide a clearer and simpler syntax for creating objects and handling inheritance.
CTo make JavaScript run faster by removing prototypes.
DTo prevent the use of objects and only allow primitive data types.
Attempts:
2 left
💡 Hint

Think about how classes help organize code and reuse features.

Predict Output
intermediate
2:00remaining
Output of class vs function object creation

What is the output of this code?

Javascript
function Person(name) {
  this.name = name;
}
Person.prototype.greet = function() {
  return `Hello, ${this.name}`;
};

class Animal {
  constructor(name) {
    this.name = name;
  }
  greet() {
    return `Hi, ${this.name}`;
  }
}

const p = new Person('Alice');
const a = new Animal('Dog');
console.log(p.greet());
console.log(a.greet());
A
Hello, Alice
Hello, Dog
B
Hi, Alice
Hello, Dog
C
Hello, Alice
Hi, Dog
D
Hi, Alice
Hi, Dog
Attempts:
2 left
💡 Hint

Look at how greet() is defined in each case.

Predict Output
advanced
2:00remaining
What happens with inheritance in classes?

What is the output of this code?

Javascript
class Vehicle {
  constructor(type) {
    this.type = type;
  }
  describe() {
    return `This is a ${this.type}`;
  }
}

class Car extends Vehicle {
  constructor(type, brand) {
    super(type);
    this.brand = brand;
  }
  describe() {
    return `${super.describe()} of brand ${this.brand}`;
  }
}

const myCar = new Car('car', 'Toyota');
console.log(myCar.describe());
ATypeError: super.describe is not a function
BThis is a Vehicle of brand Toyota
CThis is a car
DThis is a car of brand Toyota
Attempts:
2 left
💡 Hint

Check how super is used to call the parent method.

🧠 Conceptual
advanced
2:00remaining
Why classes improve code organization?

How do classes help organize code better compared to using only functions and objects?

AClasses group data and behavior together, making code easier to understand and reuse.
BClasses force all code to be written in one place, reducing modularity.
CClasses remove the need for variables and constants in code.
DClasses prevent the use of inheritance and polymorphism.
Attempts:
2 left
💡 Hint

Think about how classes bundle properties and methods.

Predict Output
expert
2:00remaining
Output of class with private fields and methods

What is the output of this code?

Javascript
class Counter {
  #count = 0;
  increment() {
    this.#count++;
  }
  getCount() {
    return this.#count;
  }
}

const c = new Counter();
c.increment();
c.increment();
console.log(c.getCount());
console.log(c.#count);
A
2
TypeError
B
2
undefined
C
2
0
D
2
SyntaxError
Attempts:
2 left
💡 Hint

Private fields cannot be accessed outside the class.