2

I am confused about the two different types of objects in JavaScript and when you are supposed to use one over the other?

Object Example 1:

 var Player = {
      firstName : 'Foo',
      lastName : 'Bar',
      getFullName : function(){
           return this.firstName + ' ' + this.lastName;
      }
 }

Object Example 2:

 var Player = function(newFirstName, newLastName){
      this.firstName = newFirstName;
      this.lastName = newLastName;
      this.getFullName = function(){
           return this.firstName + ' ' + this.lastName;
      }
 }

Is it just when you need an object to have a constructor, you would use the example 2?

Theo Kouzelis
  • 3,195
  • 5
  • 37
  • 66

0 Answers0