Object Overview

Objects

  • Objects are used to represent real world objects in programming

  • Within an object, variables are known as properties and functions are known as methods

  • Curly braces are a means of identifying if a variable is an object


// create an object that represents a dog

var myDog = {
  name: "Fido",
  age: 4,
  speak: function() {
    console.log('Woof woof');
  }
}

  • In the example above, we created an object and stored it in a variable called myDog

  • This object has 2 properties (name and age) and 1 method (speak)

This of method as actions that our objects can take


Accessing Properties and Methods of Objects

  • Properties and values can be assigned and read using Dot notation

var myDog = {
  name: "Fido",
  age: 4,
  speak: function() {
    console.log('Woof woof');
  }
}

// store myDog's name in a variable called dogName
var dogName = myDog.name;

// call the speak method of myDog 
myDog.speak();

JS Bin on jsbin.com


Updating Object properties using dot notation

  • Object properties can be updated, added or deleted

JS Bin on jsbin.com

Think of method as actions that our objects can take Like arrays, objects are another flexible and powerful feature of Javascript. Click here for more information about Objects