I ran into an issue with the naming of parameters and arguments. In the IIFE, when I pass this
as an argument, why do I need to re-name it at the parameter side.
When I rename this
, the function is able to refer to proper this
. When I do not, I'm getting unexpected token this
.
const obj = {
foo: 'bar',
met() {
(function(global) {
console.log(global); // works fine
})(this);
},
met2() {
(function(this) { // unexpected token this
console.log(this);
})(this);
},
};
obj.met();
obj.met2();
I did not understand why I'm getting an unexpected token error
.