Home › Forums › JavaScript › The use of "this" › Reply To: The use of "this"
September 13, 2013 at 3:00 pm
#150019
whatsoever
Participant
When you use a constructor function, you invoke it with the ‘new’ operator:
function Person (firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
var johnSmith = new Person('John', 'Smith');
Which puts the ‘johnSmith’ object in the context of ‘this’ in the constructor function.
The ‘add’ function takes several parameters, adds a new object to the contacts array and sets the properties of the object using object literal notation which does not require ‘this’ when setting properties.