-1

Is there any difference between "this.property" vs "var property" inside object constructor?

example:

var person = function(){
    var age;
    this.firstName;        
}
Cœur
  • 34,719
  • 24
  • 185
  • 251
user3562812
  • 1,611
  • 5
  • 19
  • 28

2 Answers2

1

Yes. For example, if you instantiate a new person like so:

var p = new person();

You will be able to access the firstName variable from outside, which becomes a property of the new object:

console.log(p.firstName); // whatever you assigned it to

But not the age variable, whose scope is limited to inside the function body:

console.log(p.age); // undefined
tckmn
  • 55,458
  • 23
  • 108
  • 154
0

this.property returns the property of the calling object. In this case, the one calling person() function.

var property just define a variable whose scope is the function person()

radically
  • 56
  • 3