Basic Javascript Flashcards
Basic class structure
class Rectangle { constructor(height, width) { this.height = height; this.width = width; } // Getter get area() { return this.calcArea(); } // Method calcArea() { return this.height * this.width; } }
are classes hoisted?
no
create new instance of class
const square = new Rectangle(10, 10);
create an async function (eg to fetch something)
async function fName() { const fetchedData = await fetch('url here'); }
default export
export default fName() {};
import default
import varName from “./fileName”;
named export
export const fName = () => {};
import named
import { fName } from “./fileName”
use Promise.all
return Promise.all([array, promises]).then((responses) => { let allDataObject = {}; allDataObject["array"] = JSON.parse(responses[0]); allDataObject["promises"] = JSON.parse(responses[1]); return allDataObject });
what does .then( … … … return x;) return?
it returns x wrapped in a promise
set an item in localstorage
window. localStorage.setItem(‘key’, ‘string value’)
note: value must be a string
get an item from localstorage
let val = window.localStorage.getItem(‘key’)
localStorage methods
localStorage.setItem(‘myCat’, ‘Tom’);
const cat = localStorage.getItem(‘myCat’);
localStorage.removeItem(‘myCat’);
localStorage.clear();
convert array/object to string
JSON.stringify(object)
convert from a string back to JSON object
JSON.parse(stringifiedObjectorArray);