Archive for January 7th, 2022|Daily archive page

Creating JavaScript Objects

2 ways: Object literals, and constructor functions.

Both use Object.create() behind the scenes. Object.create() is very verbose. The object literal, and construct functions are easier, and more readable to use.

Object Literals

let person = {
     firstName : "John",
     lastName : "Doe",
     profileColor : "Red",
     isProfileColorBlue() { return this.profileColor === "Blue"}
};

Constructor Functions

function Person(firstName, lastName, profileColor) {
     this.firstName = firstName;
     this.lastName = lastName;
     this.profileColor = profileColor;
     this.isProfileColorBlue = function() { 
          return this.isProfileColor === "Blue";
     }
}