MCQ Single Best Answer Easy

QA class's instances that are present inside of a method's body behave in a certain way according to

ID: #4989 Javascript Classes MCQ 194 views
Question Info
#4989Q ID
EasyDifficulty
Javascript Classes MCQTopic

Choose the Best Option

Click any option to instantly check if you're correct.

  • A from
  • B to
  • C this
  • D object
Correct Answer: Option C

Explanation

In object-oriented programming, the this keyword is used to refer to the current object. It is commonly used inside the body of a class's methods to refer to the object on which the method is being called.

For example, consider the following class:

class Person { constructor(name) {
this.name = name;
}

sayHello() {
console.log(`Hello, my name is ${this.name}`);
}
}

Here, the Person class has a constructor function that takes a name parameter and assigns it to the name property of the object being created.
The class also has a method called sayHello that prints a greeting to the console using the name property of the object.

Share This Question

Challenge a friend or share with your study group.