The object
is a data structure that stores other data, similar to how an array stores elements.
value
stores in an obj is associated with a key
.The Object of My Affections
In other programming languages, objects are referred to as, “dictionaries”, “maps”, or “associative arrays”.
keys
instead of numbers.{}
Fun Fact: Objects are affectionately known as POJO’s (Plain Old Javascript Objects)
Setting Keys and Values
// here "color" is the key!
> car["color"] = "Blue";
"Blue"
> car["seats"] = 2;
2
// accessing our object at the key of color
> car["color"]
"Blue"
> car["seats"]
2
> car
{color: "Blue", seats: 2}
in
operator.Using Variables as Keys
> car
{color: "Blue", seats: 2}
> let newVariable = "color";
undefined
> newVariable
"color"
> car[newVariable]
"Blue"
---
> car
{color: "Blue", seats: 2}
> newVariable
"weight"
// assigning a key value pair using a variable!
> car[newVariable] = 1000;
1000
> car
{color: "Blue", seats: 2, weight: 1000}
> let dog = {};
undefined
> dog.bark = "Bowowowo";
"Bowowowowo"
> dog.bark
"Bowowowo"
> dog
{ bark: "Bowowowowo" }
Bracket Notation vs Dot Notation
Dot | Bracket |
---|---|
Easier To Read | You can use variables as keys! |
Easier To Write b/c do not need Quotations. | Okay to use variables and Strings that start with numbers. |
Cannot access with Variables | |
Keys cannot contain numbers as their first character |
let myDog = {};
myDog.name = "Fido";
let myKey = "name";
console.log(myDog); // prints `{name: "Fido"}`
console.log(myDog[myKey]); // prints `Fido`
console.log(myDog.myKey); // prints: undefined
Putting it All Together
You can put an object together in a single statement.
Operator Precedence Revisited
Right Associativity
: When code is evaluted right to left.
Left Associativity
: When code is evaluated left to right.
Because objects store unordered key-value pairs, we do not rely on indices to access values; instead we rely on our keys.
A New Kind of For Loop
for (let variable in object) {
statement;
let obj = { name: "Rose", cats: 2 };
for (let currentKey in obj) {
console.log(currentKey);
console.log(obj[currentKey]);
}
// prints out:
// name
// cats
// Rose
// 2
}
for-in loop
.Methods vs Functions
A Method
is a function that belongs to an object. Every method is a function, but not every function is a method.
myFunc is a function
myObject.myFunc is a method of the object myObject
myObject["myFunc"] is a method of the object myObject
let dog = {
name: "Fido",
};
dog.bark = function () {
console.log("bark bark!");
};
// this is the same thing as above just using Bracket Notation
dog["speak"] = function (string) {
console.log("WOOF " + string + " WOOF!!!");
};
dog.bark(); // prints `bark bark!`
dog.speak("pizza"); // prints `WOOF pizza WOOF!!!`
let dog2 = {
name: "Rover",
bark: function () {
console.log("bork bork!");
},
speak: function (string) {
console.log("BORK " + string + " BORK!!!");
},
};
// Notice that in the object above, we still separate the key-value pairs with commas.
// `bark` and `speak` are just keys with functions as values.
dog2.bark(); // prints `bork bork!`
dog2.speak("burrito"); // prints `BORK burrito BORK!!!`
Useful Object Methods
Object.keys()
: A method that allows us to iterate through keys, it accepts an obj as the argument and returns an array of the keys.Object.values()
: Method that accepts an object as the argument and returns an array of the values.Iterating through an Object’s keys & values
Object.entries
: Method that accepts an object as the argument and returns an array of the [key,value] pairs within.Primitives vs Objects
So far we have learned about 6 different data types:
Immutabiity
Mutabulity
Using the Spread Operator and Rest Parameter Syntax Accepting Arguments
Utilizing Rest Parameters
Rest Parameter Syntax
: Allows us to capture all of a function’s incoming arguments into an array.Utilizing Spread Syntax
let numArray = [1, 2, 3];
// here we are taking `numArray` and *spreading* it into a new array where
// comma separated elements are expected to be
let moreNums = [...numArray, 4, 5, 6];
> moreNums
// => [1, 2, 3, 4, 5, 6]
With Objects
let colors = { red: "scarlet", blue: "aquamarine" };
let newColors = { ...colors };
> newColors
// { red: "scarlet", blue: "aquamarine" };
let colors = { red: "scarlet", blue: "aquamarine" };
let colors2 = { green: "forest", yellow: "sunflower" };
let moreColors = { ...colors, ...colors2 };
> moreColors
// {red: "scarlet", blue: "aquamarine", green: "forest", yellow: "sunflo
Spreading Arguments
function speak(verb, noun) {
return "I like to go " + verb + " with " + noun + ".";
}
const words = ["running", "Jet"];
console.log(speak("running", "Jet")); // => I like to go running with Jet.
console.log(speak(...words)); // => I like to go running with Jet.
Destructuring Syntax
: Allows you to extract parts of an array or obj intro distinct variables.let numArray = [10, 20];
// here we are "unpacking" the array values into two separate variables
let [firstEl, secondEl] = numArray;
console.log(firstEl); //=> 10
console.log(secondEl); //=> 20
Swapping Variables using destructuring
let num1 = 17;
let num2 = 3;
// this syntax will swap the values of the two variables
[num1, num2] = [num2, num1];
console.log(num1); // 3
console.log(num2); // 17
Destructuring objects into variables
let obj = { name: "Apples", breed: ["tabby", "short hair"] };
let { name, breed } = obj;
console.log(name); // "Apples"
console.log(breed); // ["tabby", "short hair"]
let obj = { apple: "red", banana: "yellow" };
let { apple: newApple, banana: newBanana } = obj;
console.log(newApple); // "red"
console.log(newBanana); // "yellow"
// the fname key is nested more than two levels deep
// (within bootcamp.instructor.fullName)
let bootcamp = {
name: "App Academy",
color: "red",
instructor: {
fullName: {
fname: "Rose",
lname: "K",
},
},
};
// this is hard to follow:
let {
instructor: {
fullName: { fname, lname },
},
} = bootcamp;
console.log(fname, lname);
// this is much easier to read:
let { fname, lname } = bootcamp.instructor.fullName;
console.log(fname, lname);
Destructuring and the Rest Pattern
let foods = ["pizza", "ramen", "sushi", "kale", "tacos"];
let [firstFood, secondFood, ...otherFoods] = foods;
console.log(firstFood); // => "pizza"
console.log(secondFood); // => "ramen"
console.log(otherFoods); // => ["sushi", "kale", "tacos"]
let { a, c, ...obj } = { a: 1, b: 2, c: 3, d: 4 };
console.log(a); // => 1
console.log(c); // => 3
console.log(obj); // => { b: 2, d: 4 }
We can also destructure incoming parameters of a function. This is very useful when we’re passing objects around to different functions.
let cat = { name: "Rupert", owner: "Curtis", weight: 10 };
// This unpacks the *owner* key out of any incoming object argument and
// assigns it to a owner parameter(variable)
function ownerName({ owner }) {
console.log("This cat is owned by " + owner);
}
ownerName(cat);
let bigCat = {
name: "Jet",
owner: { name: "Rose" },
toys: ["ribbon"],
siblings: { name: "Freyja", color: "orange", toys: ["mouse", "string"] },
};
// here we use *aliased* object destructuring to create a siblingToys variable
function toyFinder({ toys, siblings: { toys: siblingToys } }) {
let allToys = toys.concat(siblingToys);
return allToys;
}
console.log(toyFinder(bigCat)); // => ["ribbon", "mouse", "string"]
Benefits of Pair Programming
Prevents focusing so much on a problem that you forget to communicate with your teammates.
Helps us share knowledge faster.
Variations of Pair Programming include Mob Programming (3+), and Extreme Programming (Entire Team Rotates through Projects), all of these are known as Collaborative Programming.
Common Concepts
Pair Programming Roles
The Driver
: In charge of typing and asking questions, can let go or larger picture. Driver also suggests ways to improve/re-factor the code.The Navigator
: In charge of what’s being typed and maintaining project momentum. They should lead the discussion and also double check the code for errors as it is being typed.Shared Responsibilities
Why Pair Up?
A 5-Step Process
Partner Up
: Get your pal.Check-In
: Get to know eachother, discuss time constraints, goals, etc.Start Coding
: Drivers code, Navigators Guide.Hand Off
: App Academy employs a 15 min. interval.Follow-Up
: The last check in, this is a good time wrap up and review your code. This step helps commit your progress to memory.