- Javascript basics (MDN)
- Object basics (MDN)
- Object Terminology
-
Initialize an array using literal syntax
{}
:const fighter = { name: "Gus", strength: 8, intelligence: 3, money: null, equipment: ['sword', 'shield', 'teddy bear'], greet: function(){ return `My name is ${this.name}. Prepare to hug!` } };
-
Find a property value using dot notation.
// Gus const characterName = fighter.name;
-
Reassign a property value:
// quest completed fighter.money = 'lots';
-
Add a new property to and object:
// found cursed object fighter.weaknesses = ['peanuts','witty banter'];
-
Bonus: Add an array item to a property value.
// Add cursed object to backpack fighter.equipment[fighter.equipment.length] = 'Cursed Peanut of Akronis';
-
-
Invoke a method:
// meets creepy stranger on the road console.log(fighter.greet());
- Object properties are considered unordered, whereas arrays are ordered.
- Inside a method, the
this
keyword usually references the containing object (unless it doesn't). - Arrays and objects can be nested inside each other.
- Objects are named with nouns, methods (aka. functions) are named with verbs.