0

Philosophically, why can't I declare a new variable in JS by using this sort of code:

var this.blah = "hello"

I see that it hangs upon 'this' being a variable that has a meaning already, but -exactly- how?

what about corner cases and constructor functions?

Draconar
  • 1,147
  • 1
  • 17
  • 36
  • 1
    Because its not a variable, but a property which you are assigning to? – Bergi Sep 16 '13 at 16:42
  • it's invalid for var names to have a dot. you can do var blah = this.blah = 'hello' – dandavis Sep 16 '13 at 16:42
  • possible duplicate of [Javascript: Do I need to put this.var for every variable in an object?](http://stackoverflow.com/questions/13418669/javascript-do-i-need-to-put-this-var-for-every-variable-in-an-object) – Bergi Sep 16 '13 at 16:44
  • you can't modify/extend object definition – Grijesh Chauhan Sep 16 '13 at 16:44

1 Answers1

2

When you assign properties of an object, you never need the var keyword. That is, you should never have something like var obj.prop = ....

Trevor Dixon
  • 23,216
  • 12
  • 72
  • 109