Is there any difference between "this.property" vs "var property" inside object constructor?
example:
var person = function(){
var age;
this.firstName;
}
Is there any difference between "this.property" vs "var property" inside object constructor?
example:
var person = function(){
var age;
this.firstName;
}
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
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()