Which code should you use?

You are creating a custom object as described by the following code.

You need to implement the calcArea method.
Which code should you use?

You are creating a custom object as described by the following code.

You need to implement the calcArea method.
Which code should you use?

A.
Option A

B.
Option B

C.
Option C

D.
Option D



Leave a Reply 2

Your email address will not be published. Required fields are marked *

5 × four =


Vitalii Shevchuk

Vitalii Shevchuk

Both variants C and D are currect.
function sqr2(){
return a * a;
}

function sqr(a){
this.a = a;
this.sqr2 = sqr2;
console.log(this.sqr2());
}

sqr(2); //4
function sqr2(){
return this.a * this.a;
}

function sqr(a){
this.a = a;
this.sqr2 = sqr2;
console.log(this.sqr2());
}

sqr(2); //4

Loris

Loris

Option B is a valid method too.

function square(side) {
this.side = side;
this.area = calcArea;
}

function calcArea(obj) {
return obj.side * obj.side;
}

var sq = new square(10);
console.log(calcArea(sq));

D options requires the using of “call” method.

Which is the right answer?