๐๐ฎ๐๐ฎ๐ฆ๐ฐ๐ฟ๐ถ๐ฝ๐ ๐ข๐ฏ๐ท๐ฒ๐ฐ๐๐ ๐๐ป๐ฑ ๐ ๐ฒ๐๐ต๐ผ๐ฑ๐ ๐๐ ๐ฝ๐น๐ฎ๐ถ๐ป๐ฒ๐ฑ
A JavaScript object is a bag of named values. Use it to model the real world. Think of a car, a user, or a coffee machine.
Read properties with dot notation or brackets.
- Dot notation is shorter.
- Use brackets for keys with spaces.
- Use brackets for keys in variables.
A method is a function inside an object. The this keyword points to the object you called.
Avoid arrow functions for methods. Arrow functions do not have their own this. They take it from the outside. Use regular functions instead.
Use constructor functions to make many objects. The new keyword does a few things. It creates an empty object. It sets the properties. It returns the final object.
Put objects inside other objects for complex data. Use the optional chaining operator ?. to stop your code from crashing when a property is missing.
Use these static helpers for objects.
- Object.keys() gives you property names.
- Object.values() gives you values.
- Object.entries() gives you pairs.
- Spread operator ... copies objects.
Watch for these traps.
- Comparing objects with === checks memory addresses. It does not check contents.
- Typeof null returns object.
- Objects pass by reference. Changing an object in a function changes the original.
Build a playlist or a shopping cart to practice. Model nouns and verbs. Break things to see how they work.
Source: https://dev.to/ilyas_elaissi/javascript-objects-methods-explained-with-examples-56cg