0
0
Javascriptprogramming~5 mins

Instance methods in Javascript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an instance method in JavaScript?
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()
Where are instance methods defined in JavaScript?
AOutside the class
BInside the object literal
CInside the class body
DIn the global scope
What does 'this' refer to inside an instance method?
AThe global window object
BThe class itself
CUndefined
DThe object calling 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
Which keyword is NOT used when defining an instance method inside a class?
Afunction
BmethodName()
Cthis
Dclass
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.