Important Javascript Concepts Part3
Important Javascript Concepts Part3
JavaScript Concepts
For Interviews
Part 3
Kailash G
Explain this keyword
In JavaScript, the this keyword always refers
to an object. The thing about it is that the
object it refers to will vary depending on how
and where this is being called. If we call this
by itself, meaning not within a function,
object, or whatever, it will refer to the global
window object. The majority of the time, the
value of this depends on the runtime binding
used to call a function. It may change every
time the function is called and cannot be
changed by assignment while the function is
being executed. Although arrow functions
don't give their own this binding (it keeps the
this value of the enclosing lexical context),
the bind() method can set the value of a
function's this regardless of how it's called
1
Kailash G
Explain call(), apply() and,
bind() methods.
We use call, bind and apply methods to set the this
keyword independent of how the function is called. This is
especially useful for the callbacks.Every function object is
also given a few unique methods and attributes by
JavaScript. These are the methods that every JavaScript
function inherits. Every function inherits certain methods,
such as call, bind, and apply.
2
Kailash G
Here greeting.bind(john) creates a new function with this set
to john object, which we then assign to greetingJohn
variable. Similarly for greetingJane.
call(): The call method initializes the this inside the function
and launches it right away. In contrast to bind(), which
produces a copy of the function and sets the this keyword,
call() sets the this keyword, executes the function instantly,
and does not create a new copy of the function.
3
Kailash G
Above example is similar to the bind() example except that
call() does not create a new function. We are directly setting
the this keyword using call()
4
Kailash G
Is JavaScript single-
threaded?
JavaScript is a single-threaded asynchronous programming
language.
5
Kailash G
What are promises, async-
await and callback?
A Promise is a proxy for a value not necessarily known when
the promise is created. It allows you to associate handlers
with an asynchronous action's eventual success value or
failure reason.
6
Kailash G
Async simply allows us to write promises-based code as if it
was synchronous and it checks that we are not breaking the
execution thread. It operates asynchronously via the event
loop. Async functions will always return a value. It makes sure
that a promise is returned and if it is not returned then
JavaScript automatically wraps it in a promise which is
resolved with its value.
7
Kailash G
What is callback hell?
Callback Hell is an anti-pattern with multiple nested
callbacks which makes code hard to read and debug when
dealing with asynchronous logic.
Syntax:
async1(function(){
async2(function(){
async3(function(){
async4(function(){
....
});
});
});
});
8
Kailash G
Follow for more
great tips
Kailash G
@kailash1203
Kailash G