Question: The following JavaScript code fragments attempt to implement a counter class that hides the counter value c from direct modification. State what each code fragment

The following JavaScript code fragments attempt to implement a counter class that hides the counter value c from direct modification.
State what each code fragment actually achieves. Assume code is executed in strict mode, where variables must be declared before use.
A:
class C { constructor(c){ this.inc =()=>++c; }} let cnt = new C(1); cnt.inc =()=> c =3; cnt.inc();
B:
class C { constructor(c){ this.inc =()=>++c; Object.freeze(this); }} Object.freeze(C.prototype); let cnt = new C(2);
C:
class C { constructor(c){} inc(){ return ++c; }} let cnt = new C(2); cnt.inc();
D:
class C { constructor(){ this.c =0; } inc(){ return ++this.c; }}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!