Fireship · @fireship

What is THIS in JavaScript? in 100 seconds

Uploaded March 27, 2020 · Length 6:38

What is THIS in JavaScript? in 100 seconds

Summary

JavaScript keyword 'this' explained in 100 seconds, including execution context, strict mode, arrow functions, bind, call, and apply.

  • javascript this keyword
  • javascript execution context
  • javascript strict mode
  • javascript arrow functions
  • javascript bind method
  • javascript call method
  • javascript apply method

Transcript preview

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

  1. 0:00

    what is this in JavaScript it's a

  2. 0:02

    keyword that references another value

  3. 0:04

    usually an object that represents the

  4. 0:06

    current execution context okay but what

  5. 0:08

    the hell does execution context mean it

  6. 0:10

    either refers to your global environment

  7. 0:12

    like the browser or nodejs for when used

  8. 0:15

    inside a function it references the

  9. 0:16

    object calling that function at a given

  10. 0:18

    time

  11. 0:19

    open up the browser and run console like

  12. 0:20

    this you get the window object back

  13. 0:22

    because you're in the global execution

  14. 0:24

    context doing the same thing and nodejs

  15. 0:26

    from the command line gives you the

  16. 0:27

    global object back things get

  17. 0:29

    interesting when we talk about function

  18. 0:31

    context define a function and then

  19. 0:32

    console.log this inside the function

  20. 0:34

    body now if we run this function in the

  21. 0:36

    browser we'll still get the window

  22. 0:37

    object back that's because our global

  23. 0:39

    context is executing the function now

  24. 0:41

    let's take that same function and use it

  25. 0:43

    as a property on an object when we call

  26. 0:45

    the function on this object it

  27. 0:46

    references this object now sometimes you

  28. 0:49

    might have a function and an object

  29. 0:51

    defined separately you can tell the

  30. 0:52

    function to use some other object as its

  31. 0:54

    this value using the functions bind

  32. 0:56

    method the result is a new function

  33. 0:58

    where any references to this inside the

  34. 1:00

    function will point to the object that

  35. 1:02

    you pass as the argument here but where

  36. 1:04

    you'll see this used most often is in

  37. 1:06

    constructor functions using the new

  38. 1:07

    keyword the new keyword allows you to

  39. 1:09

    call a function that creates an object

  40. 1:11

    where this is automatically bound to

  41. 1:13

    that newly created object and that means

  42. 1:15

    you can create properties and methods

  43. 1:17

    like many other object-oriented

  44. 1:19

    programming languages for example we can

  45. 1:21

    define a property like this name then we

  46. 1:23

    can define a method like say hello and

  47. 1:25

    use this to reference the name property

  48. 1:27

    on this object and that gives us a

  49. 1:29

    convenient way to keep the data and

  50. 1:31

    functionality of an object tightly

  51. 1:32

    coupled together but there's a lot more

  52. 1:34

    to this than meets the eye so keep

  53. 1:36

    watching if you want to go beyond 100

  54. 1:37

    seconds and learn some more advanced and

  55. 1:39

    weird aspects of this but first make

  56. 1:41

    sure to hit the like button and

  57. 1:42

    subscribe

  58. 1:46

    oh hey there thanks for sticking around

  59. 1:49

    this is a special bonus segment where we

  60. 1:51

    can slow things down and take a deeper

  61. 1:53

    dive into JavaScript this which gives

  62. 1:55

    you plenty of rope to hang yourself with

  63. 1:56

    if you like this extra segment let me

  64. 1:58

    know in the comments because there's

  65. 1:59

    always room to go beyond 100 seconds