The JavaScript new operator can be used to create and initialize an object. The new operator is invoked with a constructor function, which is called to initialize the newly created object.
Consider a constructor function Circle, which is defined as:
function Circle(radius) {
this.r = radius;
}
The Circle constructor function can be called to create Circle objects c1 and c2 by using new operator:
var c1 = new Circle(5);
var c2 = new Circle(10);
There is a requirement to create a method - area() - for all Circle objects that can be invoked to get the area of a circle as e.g. c1.area()
and c2.area()
. The area of a circle for a given radius 'r' can be calculated as Math.PI*r**2
.
Which one of the below-given choices is the correct way to create the method area(), which is available to all Circle objects?