Q
How do you define a method in a constructor function?

Answer & Solution

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

What does Object.keys(obj) return?

A). An array of the values of the object's properties

B). An array of the names of the object's properties

C). The first key of the object

D). The number of properties in the object

What is the output of car.start() if var car = {start: function() {console.log('Car started');}};?

A). Car

B). started

C). Car started

D). Error

In JavaScript, what keyword is used to define a new object from a constructor function?

A). create

B). construct

C). new

D). object

How do you define a constructor function for a Car object?

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

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

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

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

What will car['make'] return if var car = {make: 'Ford', model: 'Mustang'};?

A). Ford

B). Mustang

C). Undefined

D). Error

How do you define a class in JavaScript ES6?

A). class ClassName { constructor() {} }

B). function ClassName() { constructor() {} }

C). class ClassName() { constructor {} }

D). function class ClassName { constructor() {} }

What is the purpose of the this keyword in an object method?

A). It refers to the global object.

B). It refers to the current object.

C). It refers to the parent object.

D). It refers to the previous object.

How do you add a new property to an existing object in JavaScript?

A). object.property = value;

B). object->property = value;

C). object:property = value;

D). object.addProperty('property', value);

What will the following code output? console.log(car.make); if var car = {make: 'Honda', model: 'Civic'};

A). Honda

B). Civic

C). Undefined

D). Error

What does Object.assign(target, ...sources) do?

A). Copies properties from sources to target

B). Compares properties between target and sources

C). Deletes properties in sources

D). Merges properties of sources into target and removes duplicates