fullstack_webdev

Code and notes from Full stack web developer path, LinkedIn Learning.

View on GitHub

03. Objects

03_01 Objects in JS: Explanation


03_02 JavaScript Objects: Code Version

const backpack = {
  name: "Backpack",
  volume: 30,
  color: "grey",
  pockets: 15,
  strapLength: {
    left: 25,
    right: 25,
  },
  lidOpen: false,

  // toggleLid is the name and function is the value, that takes the value lidStatus as input and sets the lidOpen = lidStatus provided
  toggleLid: function (lidStatus) {
    this.lidOpen = lidStatus; // this keyword simply refers to current object's properties
  },
  newStrapLength: function(lenLeft, lenRight){
      this.strapLength.left: lenLeft;
      this.strapLength.right: lenRight;
  }
};

03_03 Object Containers


{03_04, 03_05, 03_06}: {Object Properties, Accessing Objects, Accessing Object Properties}

  1. Dot Notation: We want to output the value one single property we can do so by doing console.log("The pocketNum value: ",backpack.pocketNum) Dot notation is the preferred way of accessing the object properties as it is easy to read and understand.

  2. Bracket Notation: In some cases, we need more control, either because we want to use a variable as the property name, or because the property name is non-standard for some reason. For this, we have bracket notation and we can use it in following manner: console.log("The pocketNum value: ", backpack["pocketNum"]


03_07 Project: Build a new Object


03_08, 03_09 Object Methods and Build a method

// Method 1

const backpack = {
  property1: "Val1",
  property2: "Val2",
  property3: "Val3",
  property4: "Val4",
  property5: "Val5",
  // Method 1 - Conventionally preferred method due to readability benefits
  method1: function (inputParam) {
    // method logic
  },
  // Method 2
  method2(inputPara) {
    // method logic
  },
};

03_10 Classes: Object Blueprints


03_11 Object Constructors


03_12 Practice: Build a new object with a constructor


03_13 Global Objects


03_14 Challenge: Create a new object type