Fireship · @fireship
What is THIS in JavaScript? in 100 seconds
Uploaded March 27, 2020 · Length 6:38
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.
- 0:00
what is this in JavaScript it's a
- 0:02
keyword that references another value
- 0:04
usually an object that represents the
- 0:06
current execution context okay but what
- 0:08
the hell does execution context mean it
- 0:10
either refers to your global environment
- 0:12
like the browser or nodejs for when used
- 0:15
inside a function it references the
- 0:16
object calling that function at a given
- 0:18
time
- 0:19
open up the browser and run console like
- 0:20
this you get the window object back
- 0:22
because you're in the global execution
- 0:24
context doing the same thing and nodejs
- 0:26
from the command line gives you the
- 0:27
global object back things get
- 0:29
interesting when we talk about function
- 0:31
context define a function and then
- 0:32
console.log this inside the function
- 0:34
body now if we run this function in the
- 0:36
browser we'll still get the window
- 0:37
object back that's because our global
- 0:39
context is executing the function now
- 0:41
let's take that same function and use it
- 0:43
as a property on an object when we call
- 0:45
the function on this object it
- 0:46
references this object now sometimes you
- 0:49
might have a function and an object
- 0:51
defined separately you can tell the
- 0:52
function to use some other object as its
- 0:54
this value using the functions bind
- 0:56
method the result is a new function
- 0:58
where any references to this inside the
- 1:00
function will point to the object that
- 1:02
you pass as the argument here but where
- 1:04
you'll see this used most often is in
- 1:06
constructor functions using the new
- 1:07
keyword the new keyword allows you to
- 1:09
call a function that creates an object
- 1:11
where this is automatically bound to
- 1:13
that newly created object and that means
- 1:15
you can create properties and methods
- 1:17
like many other object-oriented
- 1:19
programming languages for example we can
- 1:21
define a property like this name then we
- 1:23
can define a method like say hello and
- 1:25
use this to reference the name property
- 1:27
on this object and that gives us a
- 1:29
convenient way to keep the data and
- 1:31
functionality of an object tightly
- 1:32
coupled together but there's a lot more
- 1:34
to this than meets the eye so keep
- 1:36
watching if you want to go beyond 100
- 1:37
seconds and learn some more advanced and
- 1:39
weird aspects of this but first make
- 1:41
sure to hit the like button and
- 1:42
subscribe
- 1:46
oh hey there thanks for sticking around
- 1:49
this is a special bonus segment where we
- 1:51
can slow things down and take a deeper
- 1:53
dive into JavaScript this which gives
- 1:55
you plenty of rope to hang yourself with
- 1:56
if you like this extra segment let me
- 1:58
know in the comments because there's
- 1:59
always room to go beyond 100 seconds


