letmyDog={name: 'Keeva',color: 'Black and White',age: 3,isMale: false,isFemale: true};// Accessing the name property on the myDog object uses the "." operatorconsole.log(myDog.name);// Accessing the color property on the myDog object uses "[]"console.log(myDog['color']);
Using functions and 'this'
letuser={name: "Chris",interests: ["videogames","hockey","coding","roblox development"],isInterested(topic){if(this.interests.includes(topic)){console.log('${this.name} is interested in ${topic}')}else{console.log('${this.name} is not interested in ${topic}')}}}// Call the functionuser.isInterested("hockey")// If true: "Chris is interested in hockey"user.isInterested("boring stuff")// If false: "Chris is not interested in boring stuff"
Node and NPM Review
To access the javascript ecosystem, you need to access it through NPM (Node Package Manager)
To check that you have npm installed, type 'npm -v' in to a cmd line
To use npm in your projects, you'll need to start a project with 'npm init'
The command line scripts that you have access to are under "scripts".
"devDependencies" are packages that you have access to in your dev environment
"dependencies" are packages that you have access to everywhere
ES Modules Review
// food.js fileconstFOOD_THOUGHTS={'sushi': 30,// many more}constgetFoodThoughtKeys=()=>{returnObject.keys(FOOD_THOUGHTS)}constgetFoodThoughtsValues=()=>{returnObject.values(FOOD_THOUGHTS)}export{getFoodThoughtKeys,getFoodThoughtsValues}// index.js file (in the same folder as food.js)// How to import files locally:import{getFoodThoughtKeys,getFoodThoughtsValues}from'./food.js'// Call the functionsgetFoodThoughtKeys()getFoodThoughtsValues()
Fetch Review
// Source https://github.com/HackerNews/API#itemsconstSTORY_URL="https://hacker-news.firebaseio.com/v0/item/8863.json"fetch(STORY_URL).then((response)=>{returnresponse.json()}).then((storyObject)=>{console.log(storyObject.title)console.log(storyObject.url)})// Written as an async functionconstgetStory=async()=>{letresponse=awaitfetch(STORY_URL)letstoryObject=awaitresponse.json()console.log(storyObject.title)console.log(storyObject.url)}getStory()
Custom Promises Review
constcompleteHomework=(didDoHomework)=>{returnnewPromise((resolve,reject)=>{if(didDoHomework){// It takes us 3 seconds to do itsetTimeout(()=>{resolve("You can go out");},3000);}else{// If we didn't do our homework, we'll studyreject("You need to study");}});}console.log("Can you go out with friends on Monday?")completeHomework(true).then((result)=>{console.log(result);});// This is a positive result, so we can go out// NOTE: We used '.then()' to process positive resultsconsole.log("Can you go out with friends on Tuesday?")comepleteHomework(false).catch((result)=>{console.log(result);});// This is a negative promise result, so we cannot go out :(// NOTE: We used '.catch()' to process negative results (catches errors)
Async and Await Review
letrandomDogURL="https://dog.ceo/api/breeds/image/random";constgetRandomDogPicture=async()=>{// We await the response of fetchletresponse=awaitfetch(randomDogURL);// We use await to parse the bodyletjson_data=awaitresponse.json();//Take a look at the objectconsole.log(json_data);returnjson_data.message;}// Call the async function in an IIFEso you can use await(asyncfunction(){letpicture=awaitgetRandomDogPicture();document.querySelector("img").src=picture;})();