Fireship · @fireship

An Encounter with JavaScript Objects

Uploaded October 18, 2019 · Length 10:38

An Encounter with JavaScript Objects

Summary

This video teaches how to work with JavaScript objects, including creating them, setting properties, and using modern syntax and methods to make code cleaner and more efficient.

  • javascript objects
  • object literal syntax
  • constructor syntax
  • object create
  • defined property
  • object destructuring
  • object methods
  • this keyword
  • object references

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    [Music]

  2. 0:01

    you unlock this door with the key of

  3. 0:04

    imagination this is an object a

  4. 0:06

    collection of properties where each

  5. 0:08

    property associate say key to a value

  6. 0:10

    earlier in the course we learned that

  7. 0:12

    there are seven primitive data types in

  8. 0:13

    Java Script primitives represent a

  9. 0:15

    single value like a number or a string

  10. 0:17

    and anything that's not a primitive is

  11. 0:19

    an object in other words everything

  12. 0:21

    interesting that happens in JavaScript

  13. 0:22

    is made possible by the object datatype

  14. 0:28

    today we'll take a deep dive into how

  15. 0:30

    the JavaScript object works and look at

  16. 0:32

    modern syntax and methods we can use to

  17. 0:34

    make our code cleaner and more efficient

  18. 0:35

    if you're new here like and subscribe

  19. 0:37

    and grab all of the videos on this free

  20. 0:39

    JavaScript course from bioship io an

  21. 0:41

    object is conceptually similar to a

  22. 0:43

    dictionary or a map that you might find

  23. 0:45

    in other programming languages the most

  24. 0:47

    straightforward way to create an object

  25. 0:48

    is to use the literal syntax simply

  26. 0:51

    define a variable equal to curly braces

  27. 0:53

    inside the braces you define properties

  28. 0:56

    separated by commas a property is

  29. 0:59

    essentially a key value pair where the

  30. 1:00

    left side is the property name and the

  31. 1:02

    right side is the property value the

  32. 1:05

    property name needs to be a unique value

  33. 1:07

    that can be coerced to a string the

  34. 1:09

    value can be anything you want a

  35. 1:10

    primitive another object a function or

  36. 1:13

    whatever now even though we define this

  37. 1:15

    object as a const variable we can still

  38. 1:17

    set properties on it after it's been

  39. 1:19

    created we can use bracket notation on

  40. 1:21

    the object followed by the name of the

  41. 1:23

    property equal to whatever value we want

  42. 1:25

    to set there then later in the code we

  43. 1:27

    can read or get that property using the

  44. 1:29

    same bracket notation

  45. 1:32

    now let's imagine that we've walked into

  46. 1:34

    an alternate universe because in

  47. 1:36

    JavaScript there is always more than one

  48. 1:37

    way to do things

  49. 1:38

    this time we'll create a new object

  50. 1:40

    using the constructor syntax then we'll

  51. 1:42

    set properties on it using dot notation

  52. 1:44

    dot notation looks a lot cleaner but

  53. 1:46

    your property names will need to be a

  54. 1:48

    continuous string that does not start

  55. 1:50

    with a number if you have property names

  56. 1:51

    with spaces or that start with numbers

  57. 1:53

    you'll get a syntax error if you try to

  58. 1:55

    call them with dot notation but now

  59. 1:58

    we've fallen into yet another alternate