I came across an interview question today, but I didn't figure out how to solve it.

var a1 = new A();
console.log(a1.val); //1
var a2 = new A();
console.log(a2.val);//2

I was thinking of using closures, but I didn"t write them. Can anyone help me to answer them?

Dec.22,2021


function A(){
    this.val=A.prototype.valPP
}
A.prototype.val=1
let a1=new A()
let a2=new A()
console.log(a1.val, a2.val)
Menu