Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript The use of "this" Reply To: The use of "this"

#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.