Q
How do you add a method to an object created with a constructor function?

Answer & Solution

Answer: Option A
Solution:
Methods in a constructor function are added using this.methodName = function() {};.
Related Questions on Average

How do you define a method inside a class in JavaScript ES6?

A). methodName() {}

B). this.methodName = function() {}

C). function methodName() {}

D). methodName: function() {}

How can you delete the age property from the person object?

A). delete person.age;

B). remove person.age;

C). person.age = null;

D). person.age.delete;

What will console.log(car.make); output if var car = {make: 'Toyota', model: 'Corolla'};?

A). Toyota

B). Corolla

C). Undefined

D). Error

What is the purpose of the this keyword in a constructor function?

A). It refers to the new object being created.

B). It refers to the global object.

C). It refers to the current object.

D). It refers to the parent object.

How do you call a method from a parent class in a subclass in JavaScript ES6?

A). super.methodName()

B). this.methodName()

C). parent.methodName()

D). base.methodName()

What is the correct way to create an object in JavaScript?

A). var obj = {};

B). var obj = [];

C). var obj = ();

D). var obj = <>;

How do you create an instance of the Car object using the constructor function?

A). var myCar = new Car('Toyota', 'Corolla');

B). var myCar = Car('Toyota', 'Corolla');

C). var myCar = create Car('Toyota', 'Corolla');

D). var myCar = construct Car('Toyota', 'Corolla');

What is the output of console.log(person.greet()); if var person = {greet: function() {return 'Hello';}};?

A). Hello

B). Undefined

C). Error

D). Hello

How do you inherit properties and methods from another class in JavaScript ES6?

A). class SubClass extends SuperClass {}

B). class SubClass inherits SuperClass {}

C). class SubClass implements SuperClass {}

D). class SubClass super SuperClass {}

How do you define a constructor function for creating Car objects?

A). function Car(make, model) { this.make = make; this.model = model; }

B). Car(make, model) { this.make = make; this.model = model; }

C). var Car = function(make, model) { this.make = make; this.model = model; };

D). Car = function(make, model) { this.make = make; this.model = model; };