In JavaScript, the this
keyword refers to the execution context in which code is currently running. It can have different values depending on how it is used.
this
in a FunctionWhen this
is used inside a function, its value depends on how the function is called.
index.tsx152 chars11 lines
In this example, this
inside the greet()
function refers to the person
object because the function is called on that object.
this
in Object-Oriented ProgrammingIn object-oriented programming, this
is often used to refer to the current object instance.
index.tsx256 chars14 lines
In this example, this
inside the drive()
method refers to the current Car
instance that the method is being called on.
this
in a Different ContextYou can use the call()
or apply()
methods to set the value of this
explicitly for a function invocation.
index.tsx218 chars10 lines
In this example, call()
is used to specify that this
should refer to person1
or person2
.
gistlibby LogSnag