An instance method is a function defined inside a class that can be called on objects created from that class. It operates on the data (properties) of the specific object.
Click to reveal answer
beginner
How do you define an instance method inside a JavaScript class?
Inside a class, you write a function without the 'function' keyword. For example:<br><pre>class Dog {
bark() {
console.log('Woof!');
}
}</pre>
Click to reveal answer
beginner
Why do instance methods use 'this' keyword?
'this' refers to the specific object calling the method. It lets the method access or change that object's properties.
Click to reveal answer
beginner
Can instance methods access properties of the object? How?
Yes, instance methods access properties using 'this.propertyName'. For example, if an object has a 'name' property, the method can use 'this.name' to get or set it.
Click to reveal answer
beginner
What happens if you call an instance method without creating an object?
You get an error because instance methods belong to objects, not the class itself. You must create an object (instance) first to use them.
Click to reveal answer
How do you call an instance method named 'greet' on an object 'person'?
Aperson.greet()
Bgreet.person()
Cperson->greet()
Dgreet()
✗ Incorrect
You call instance methods using the object name followed by a dot and the method name with parentheses.
Where are instance methods defined in JavaScript?
AOutside the class
BInside the object literal
CInside the class body
DIn the global scope
✗ Incorrect
Instance methods are defined inside the class body so all objects created from the class can use them.
What does 'this' refer to inside an instance method?
AThe global window object
BThe class itself
CUndefined
DThe object calling the method
✗ Incorrect
'this' inside an instance method points to the specific object that called the method.
What happens if you try to call an instance method directly on the class without creating an object?
AMethod runs normally
BError or undefined behavior
CCreates a new object automatically
DReturns the method code as a string
✗ Incorrect
Instance methods require an object to work on. Calling them on the class itself causes an error.
Which keyword is NOT used when defining an instance method inside a class?
Afunction
BmethodName()
Cthis
Dclass
✗ Incorrect
Inside a class, instance methods are defined without the 'function' keyword.
Explain what an instance method is and how it uses 'this' in JavaScript classes.
Think about how methods belong to objects created from classes.
You got /3 concepts.
Describe the steps to create a class with an instance method and call that method on an object.
Remember the order: class, method, object, call.
You got /4 concepts.
Practice
(1/5)
1. What is an instance method in JavaScript classes?
easy
A. A special keyword to create new objects
B. A variable that stores data inside a class
C. A function that runs without creating an object
D. A function defined inside a class that works with object data
Solution
Step 1: Understand class components
A class can have variables (properties) and functions (methods).
Step 2: Identify instance methods
Instance methods are functions inside the class that use object data.
Final Answer:
A function defined inside a class that works with object data -> Option D
Quick Check:
Instance method = function inside class using object data [OK]
Hint: Instance methods are functions inside classes using object info [OK]
Common Mistakes:
Confusing instance methods with variables
Thinking instance methods run without objects
Mixing instance methods with static methods
2. Which of the following is the correct syntax to define an instance method greet inside a JavaScript class?
easy
A. function greet() { console.log('Hello'); }
B. greet() { console.log('Hello'); }
C. var greet = () => { console.log('Hello'); }
D. static greet() { console.log('Hello'); }
Solution
Step 1: Recall instance method syntax
Inside a class, instance methods are defined by name followed by parentheses and braces without the function keyword.
Step 2: Check each option
greet() { console.log('Hello'); } matches this syntax. function greet() { console.log('Hello'); } wrongly uses function. var greet = () => { console.log('Hello'); } uses variable syntax outside class method style. static greet() { console.log('Hello'); } defines a static method, not instance.
class Person {
constructor(name) {
this.name = name;
}
greet() {
return `Hi, I am ${this.name}`;
}
}
const p = new Person('Anna');
console.log(p.greet());
medium
A. Hi, I am Anna
B. Hi, I am undefined
C. Error: greet is not a function
D. Hi, I am p
Solution
Step 1: Understand object creation
The Person class sets this.name to 'Anna' when creating p.
Step 2: Call the instance method
The greet method returns a string using this.name, which is 'Anna'.
Final Answer:
Hi, I am Anna -> Option A
Quick Check:
Method uses this.name = 'Anna' so output matches [OK]
Hint: Instance methods use 'this' to access object data [OK]
Common Mistakes:
Forgetting to use 'this' inside methods
Calling method without creating object
Expecting method to print instead of return
4. Find the error in this code:
class Car {
constructor(model) {
this.model = model;
}
showModel() {
console.log(model);
}
}
const c = new Car('Tesla');
c.showModel();
medium
A. Constructor should not have parameters
B. showModel should return a value, not console.log
C. Missing 'this' before model in showModel method
D. Cannot create object with 'new' keyword
Solution
Step 1: Check variable usage in method
The method tries to print model but it is not defined locally or globally.
Step 2: Use 'this' to access object property
Inside methods, object properties must be accessed with this.model.
Final Answer:
Missing 'this' before model in showModel method -> Option C
Quick Check:
Access object data with this.property [OK]
Hint: Use 'this' to access object properties inside methods [OK]
Common Mistakes:
Omitting 'this' before property names
Confusing local variables with object properties
Thinking constructor parameters are not allowed
5. You want to create a class Counter that counts how many times its method increment is called on each object separately. Which code correctly implements this behavior?
hard
A. class Counter {
constructor() {
this.count = 0;
}
increment() {
this.count++;
return this.count;
}
}
B. class Counter {
static count = 0;
increment() {
Counter.count++;
return Counter.count;
}
}
C. class Counter {
constructor() {
this.count = 0;
}
increment() {
count++;
return count;
}
}
D. class Counter {
increment() {
this.count = this.count + 1;
return this.count;
}
}
Solution
Step 1: Understand instance vs static properties
Each object needs its own count, so it must be an instance property set in the constructor.
Step 2: Check increment method
The method should increase this.count and return it. class Counter {
constructor() {
this.count = 0;
}
increment() {
this.count++;
return this.count;
}
} does this correctly.
Step 3: Identify errors in other options
class Counter {
static count = 0;
increment() {
Counter.count++;
return Counter.count;
}
} uses static property shared by all objects. class Counter {
constructor() {
this.count = 0;
}
increment() {
count++;
return count;
}
} uses undefined count variable. class Counter {
increment() {
this.count = this.count + 1;
return this.count;
}
} does not initialize count before incrementing.
Final Answer:
class Counter {
constructor() {
this.count = 0;
}
increment() {
this.count++;
return this.count;
}
} -> Option A
Quick Check:
Instance property in constructor + method updates it [OK]
Hint: Instance properties track data per object; static shares across all [OK]
Common Mistakes:
Using static properties for per-object data
Not initializing properties before use
Incrementing undefined variables
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.