Quizz
The full text of this chapter, to read or revisit. To practise, do the exercises.
βΆ Do the exercisesExercise 1 β Quiz: Constructor Property
What does this code print?
class Item {
constructor(name) {
this.name = name;
}
}
const p = new Item("Potion");
console.log(p.name);PotionnameundefinedHint : Follow the parameter to the property it fills.
Exercise 2 β Quiz: Computed in the Constructor
What does this code print?
class Codemon {
constructor(level) {
this.hp = level * 10;
}
}
console.log(new Codemon(4).hp);40410undefinedHint : The constructor can compute a value from its parameters.
Exercise 3 β Quiz: Write the Constructor
Complete the class so each instance gets a name.
Hint : One special method runs at creation, one keyword names the object being built.
Exercise 4 β Quiz: Separate Instances
What does the last line print?
class C {
constructor() {
this.n = 0;
}
}
const a = new C();
const b = new C();
a.n = 9;
console.log(b.n);09undefinedHint : Each new builds its own object.
Exercise 5 β Quiz: Method Result
What does this code print?
class Item {
constructor(price) {
this.price = price;
}
double() {
return this.price * 2;
}
}
console.log(new Item(15).double());3015undefinedHint : The method reads the instance's property and computes.
Exercise 6 β Quiz: No Commas
How are two methods separated inside a class body?
Hint : Unlike an object literal, a class body has no separators between methods.
Exercise 7 β Quiz: Add a Method
Give the class a `shout` method that returns the name in uppercase.
Hint : The method name matches the call, and the instance is reached through one keyword.
Exercise 8 β Quiz: Method Changes the Object
What does the last line print?
class C {
constructor() {
this.n = 0;
}
add() {
this.n = this.n + 5;
}
}
const c = new C();
c.add();
c.add();
console.log(c.n);1050undefinedHint : Each call updates the same instance.
Exercise 9 β Quiz: Missing new
What happens when this runs?
class C {}
const x = C();undefinedHint : A class cannot be called like a plain function.
Exercise 10 β Quiz: instanceof
What does this code print?
class A {}
class B {}
const x = new A();
console.log(x instanceof B);falsetrueundefinedHint : An instance remembers exactly which class built it.
Exercise 11 β Quiz: Build and Check
Create an instance and check its class.
Hint : One keyword presses the stamp, one operator asks which stamp was used.
Exercise 12 β Quiz: Instances From map
What does this code print?
class Item {
constructor(n) {
this.n = n;
}
}
const items = ["a", "b"].map((n) => new Item(n));
console.log(items.length + items[1].n);2b2aabHint : One instance per source element, then read the second one's property.
Exercise 13 β Quiz: this in a Method
What does this code print?
class Codemon {
constructor(name) {
this.name = name;
}
who() {
return this.name;
}
}
const a = new Codemon("Zap");
const b = new Codemon("Aquachu");
console.log(b.who());AquachuZapundefinedHint : this is the object before the dot in the call.
Exercise 14 β Quiz: Chained Methods
What does the last line print?
class C {
constructor() {
this.n = 0;
}
add() {
this.n++;
return this;
}
}
const c = new C();
c.add().add();
console.log(c.n);210undefinedHint : Each call returns the same instance for the next call.
Exercise 15 β Quiz: Return the Instance
Make the method chainable.
Hint : Hand the instance itself back so the next call lands on it.
Exercise 16 β Quiz: Arrow Callback
Inside a class method, which kind of callback keeps `this` pointing at the instance?
Hint : One kind of function takes the this of the place it was written in.
Exercise 17 β Quiz: Inherited Method
What does this code print?
class A {
hi() {
return "hi";
}
}
class B extends A {}
console.log(new B().hi());hiundefinedBHint : The child keeps every method of the parent.
Exercise 18 β Quiz: super in the Constructor
What does this code print?
class A {
constructor(x) {
this.x = x;
}
}
class B extends A {
constructor(x, y) {
super(x);
this.y = y;
}
}
const b = new B(1, 2);
console.log(b.x + b.y);312undefinedHint : The parent sets one property, the child adds the other.
Exercise 19 β Quiz: Extend and Call
Make `B` inherit from `A` and run the parent's constructor first.
Hint : One keyword links the classes, another runs the parent's constructor.
Exercise 20 β Quiz: Override
What does this code print?
class A {
hi() {
return "A";
}
}
class B extends A {
hi() {
return "B";
}
}
console.log(new B().hi());BAABHint : The child's version wins for the child's instances.
Exercise 21 β Quiz: super.method()
What does this code print?
class A {
hi() {
return "A";
}
}
class B extends A {
hi() {
return super.hi() + "B";
}
}
console.log(new B().hi());ABBABAHint : The child runs the parent's version first, then adds its own.
Exercise 22 β Quiz: instanceof the Parent
What does this code print?
class A {}
class B extends A {}
const b = new B();
console.log(b instanceof A);truefalseundefinedHint : A child instance is also an instance of the parent.
Exercise 23 β Quiz: super Before this
In a child constructor, what must happen before using `this`?
Hint : The object isn't ready until the parent has built its part.
Exercise 24 β Quiz: Why a Class?
What is the main reason to use a class instead of writing object literals by hand?
Hint : Think about repeating the same shape many times.
Exercise 25 β Quiz: Capital Letter
Why are class names written with a capital letter?
Hint : It's about what the reader expects, not what the language enforces.