Guess the output
Bind vs Call
The bind()
method creates a new function that, when called, has its this
keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called. [Source Mozilla]
The call()
method calls a function with a given this
value and arguments provided individually.
So when we are using Bind the output is return Function. [Fix: console.log(helloBuddy.bind(person,’UI Developer’)())]
Caution while using if statement in Javascript
Well, I had personally faced this issue many times Javascript logics are pretty tough to understand, let’s see why randomUser is a number but else console shows the else condition. (!typeof randomUser ===‘string’) will be broken into 2 parts -> typeof randomUser ===‘string’ comes out to be true but at same type ‘!’ will change that true to false so if(false) will never be executed. Trust me how much experience you have you will fall into these conditions very frequently.
this and arrow function
An arrow function expression is a compact alternative to a traditional function expression, but is limited and can’t be used in all situations.
Differences & Limitations:
- Does not have its own bindings to
this
orsuper
, and should not be used asmethods
. - Does not have
arguments
, ornew.target
keywords. - Not suitable for
call
,apply
andbind
methods, which generally rely on establishing a scope. - Can not be used as constructors.
- Can not use
yield
, within its body.
Same was this in arrow function is having the reference of global scope. When we use this.email = email, the variable in global scope is set.
Sources:
https://developer.mozilla.org
https://www.youtube.com/channel/UCHx46rNCTVKAK4YY1A1eBAw