Create an Object:
let spaceShip = {
'Fuel Type': 'Turbo Fuel',
color: 'silver'
};

Accessing Objects:
spaceShip["Fuel Type"]; // 'Turbo Fuel' 
spaceShip.color; // 'silver' 

Property Assignment:
spaceShip.type = 'Alien'; // to reassign values or to create a new key:value pair if non-existent 

Delete:
delete spaceShip.color; // to delete a key:value pair 

Object Methods:
const alienShip = {
invade(){console.log('Hello! We have come to dominate your planet!')}
};
alienShip.invade(); // Prints 'Hello! We have come to dominate your planet!'

Nested Objects:
const spaceship = {
    telescope: {
       yearBuilt: 2018,
       model: '91031-XLT',
       focalLength: 2032 }, ...
};

Objects, { }, are collections of related data and constist of key:value pairs. Can have their own methods().

const factory = (name, age, energy, phrase) => {
    return { 
        name: name,
        age: age, 
        energy: energy,
        say() {
            console.log(phrase);
        } 
    }
};
const newObj = factory('Joe', 100, 'power', 'Welcome!'); 
newObj.say(); // 'Welcome!'

To create many instances of objects quickly.

const factory = (name, age) => {
  return { 
    name,  // name: name,
    age    // age: age
  }
};

Don’t need to add a key name, just use value to have the same key and values names.

const messages = {
    name: "Joe",
    greet() {return "Hi " + this.name + "!"}
};
messages.greet(); // Hi Joe! 

Refers to the calling object, avoid using arrow function when using this.

const object = { a: 1, b: 2, c: 3 };

for (const key in object) {
  console.log(`${key}: ${object[key]}`);
}

// Expected output:
// "a: 1"
// "b: 2"
// "c: 3"

The for in statement loops through the properties of an Object or Array.

Object.keys(object); // To get the key names of an object 
const object = {
  a: 1,
  b: 2,
  c: 3
};
console.log(Object.keys(object)); // Expected output: Array ["a", "b", "c"] 

Object.entries(object); // To get both key names and values of an object
const object = {
  a: 1,
  b: 2
};

for (const [key, value] of Object.entries(object)) {
  console.log(`${key}: ${value}`);
}
// Expected output:
// "a: 1"
// "b: 2"

Object.assign(object); // Copies all properties of a source object to a target object 
const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 };

const returnedTarget = Object.assign(target, source);

console.log(target); 
// Expected output: Object { a: 1, b: 4, c: 5 }
console.log(returnedTarget === target); 
// Expected output: true 

Some methods to be used on objects.

get
const person = {
  _firstName: 'John',
  _lastName: 'Doe',
  get fullName() {
    if (this._firstName && this._lastName){
      return `${this._firstName} ${this._lastName}`;
    } else {
      return 'Missing a first name or a last name.';
    }
  }
}
To call the getter method:
person.fullName; // 'John Doe'

Methods preprended with the keyword get. Are methods that get and return the internal properties of an object. Use underscore convention _objectName, to mean that property should not be altered.

set
const person = {
  _age: 37,
  set age(newAge){
    if (typeof newAge === 'number'){
      this._age = newAge;
    } else {
      console.log('You must assign a number to age');
    }
  }
};
To use a setter method:
person.age = 40;
console.log(person._age); // Logs: 40
person.age = '40'; // Logs: You must assign a number to age 

Along with getter methods, we can also create setter methods which reassign values of existing properties within an object.

const object = {
a: 1,
b: {
  bOne: 2.1,
  bTwo: 2.2
 }
};

const { a } = object; // 1
const { bOne, bTwo } = object.b; // 2.1, 2.2
const { a, b: { bOne, bTwo} } = object; // both 1, 2.1, 2.2 

Shorthand property to get values from objects.

const ship = {
  captain : 'Jack Sparrow',
  flagType : 'Pirate'
};
 
let changeFlag = obj => {
  obj.flagType = 'Old Navy'
};
 
changeFlag(ship);
ship.flagType // Returns 'Old Navy'

In pass by reference, parameters passed as an arguments does not create its own copy, it refers to the original value so changes made inside function affect the original value.