- An object stores data as key: value pairs called properties:
{ name: "Omar", age: 17 }. - Read properties with dot notation (
obj.key) or bracket notation (obj["key"]orobj[variable]). - Missing properties give
undefined. Reading a property ofundefinedthrows a TypeError. - Add or change properties by assigning to them. Remove them with
delete. Check for them within. - A function stored in an object is a method. Inside a method,
thisrefers to the object. Don't use arrow functions as methods. - Objects and arrays can be nested inside each other. Arrays of objects are the most common data shape.
- Loop over keys with
for...in, or useObject.keys,Object.valuesandObject.entries. - Shorthand:
{ name }means{ name: name }. Destructuring:const { name, age } = person; - Objects are stored by reference.
b = ashares the object. Copy with{ ...a }, merge with{ ...a, ...b }. - JSON is text that represents objects. Use
JSON.stringifyandJSON.parseto convert.
Reading is step one
CurlyScript turns these notes into a game: short questions, an explanation after every answer, and a test at the end of each grade. Free, with nothing to sign up for.