Challenge - 5 Problems
Instance Methods Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of instance method call
What is the output of this JavaScript code?
Javascript
class Car { constructor(make) { this.make = make; } getMake() { return `This car is a ${this.make}`; } } const myCar = new Car('Toyota'); console.log(myCar.getMake());
Attempts:
2 left
💡 Hint
Remember that instance methods use 'this' to access properties of the object.
✗ Incorrect
The method getMake() returns a string using the 'make' property of the instance. Since 'myCar' was created with 'Toyota', the output is 'This car is a Toyota'.
❓ Predict Output
intermediate2:00remaining
Value of property after method call
What is the value of 'counter.count' after running this code?
Javascript
class Counter { constructor() { this.count = 0; } increment() { this.count += 1; } } const counter = new Counter(); counter.increment(); counter.increment();
Attempts:
2 left
💡 Hint
Each call to increment adds 1 to the count property.
✗ Incorrect
The constructor sets count to 0. Each increment call adds 1, so after two calls, count is 2.
❓ Predict Output
advanced2:00remaining
Output with method using arrow function
What is the output of this code?
Javascript
class Person { constructor(name) { this.name = name; } greet = () => { return `Hello, my name is ${this.name}`; } } const p = new Person('Alice'); console.log(p.greet());
Attempts:
2 left
💡 Hint
Arrow functions keep the 'this' from their surrounding context.
✗ Incorrect
The greet method is an arrow function property, so 'this.name' correctly refers to the instance property 'name'. The output is 'Hello, my name is Alice'.
❓ Predict Output
advanced2:00remaining
Output when method is extracted from instance
What is the output of this code?
Javascript
class Dog { constructor(name) { this.name = name; } bark() { return `${this.name} says woof!`; } } const dog = new Dog('Buddy'); const barkFn = dog.bark; console.log(barkFn());
Attempts:
2 left
💡 Hint
When a method is called without its object, 'this' is undefined in strict mode.
✗ Incorrect
Extracting the method and calling it loses the original 'this' binding. So 'this.name' is undefined, resulting in 'undefined says woof!'.
🧠 Conceptual
expert2:00remaining
Why use instance methods instead of static methods?
Which is the best reason to use instance methods instead of static methods in a class?
Attempts:
2 left
💡 Hint
Think about what data each method type can access.
✗ Incorrect
Instance methods work with data specific to each object instance. Static methods belong to the class itself and cannot access instance properties.