I'm reading typescript output now. It converts modern ES21 to the older versions. I am wondering why It's this way, I mean why devs used to create a whole variable _this
and set it to this
keyword. Like it's easier to just put this
instead of create a new variable... Any ideas?
Input (TypeScript)
const Singleton = () => {
if (Singleton._instance) return Singleton._instance;
Singleton._instance = this;
return this;
};
const a = new Singleton();
const b = new Singleton();
console.log(a === b);
Output (JavaScript)
// BUT WHY ?
var _this = this;
var Singleton = function () {
if (Singleton._instance)
return Singleton._instance;
Singleton._instance = _this;
return _this;
};
var a = new Singleton();
var b = new Singleton();
console.log(a === b);