1

What is the difference between declaring a variable with this in my function than declaring normally using let or var?

const controller = (function() {
  this.name = 'John';
})();

const controller2 = (function() {
  let name = 'Mary';
})();
Ivan
  • 34,531
  • 8
  • 55
  • 100
gian29
  • 710
  • 2
  • 7
  • 9
  • 2
    `this` belongs to objects, it doesn't declare any variables. Also, the provided examples don't make sense, both controller variables are assigned with `undefined`. – Teemu Jun 21 '18 at 09:12
  • [This will help you about let and this](https://stackoverflow.com/questions/762011/whats-the-difference-between-using-let-and-var-to-declare-a-variable-in-jav) – tklanilkumar Jun 21 '18 at 09:15

1 Answers1

3

this inside your function is window object. So you are not creating a variable, you are adding a property to the window object

console.log((function(){return this})() === window)
Francesco
  • 4,052
  • 2
  • 21
  • 29