I tried below code to understand IIFE behaviour inside class methods. Intrestingly I get undefined when I try to print the variable inside IIFE.
window.numValue=0;
class ArOps {
constructor (){
this.numValue = 0;
}
add(){
this.numValue += 10;
console.log(this.numValue);
(function(){
console.log(this.numValue);
})();
}
}
let arOps = new ArOps();
arOps.add();
Even though window object contains the 'numValue' variable, console.log throws error
window.numValue=0;
class ArOps {
constructor (){
this.numValue = 0;
}
add(){
this.numValue += 10;
console.log(this.numValue);
(function(){
console.log(this.numValue);
}).call(window);
}
}
let arOps = new ArOps();
arOps.add();
Can anyone help me understand the reason for initial error?