-1

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

example:

var person = function(){
    var age;
    this.firstName;        
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
user3562812
  • 1,751
  • 5
  • 20
  • 30

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
  • 57,719
  • 27
  • 114
  • 156
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