To my surprise, I wrote the code,
function printThis() {
console.log(this);
}
const f = printThis.bind(5).bind(7);
f(); // prints 5 instead ...
For further actions, you may consider blocking this person and/or reporting abuse
When you use bind it does not run your function just it sets the data 5,
So,
printThis.bind(5)set 5 to 'this' then it does not return the exact function becuse it returns prototype undefined.So you cannot use again, you can think like a constant variable, it's set one time then you can not set again.
As a result, I write a function rbind means 're bind'.
It simply run the function first then returns the function which is not changed.
That is interesting, I can check if prototype is undefined, that means function is already bound.
No it doesn't mean that necessarily. Somebody could explicitly set the prototype to undefined for instance.
So if .prototype is already undefined it means that the function cannot be chained, right?
This is a HORRIBLE interview question, not once in 20 years have I ever had to address anything like this.
To play devil's advocate, I will disagree with this statement.
While you will never run across code that looks like this:
It is very possible (#1) you might run across a function that has been previously bound. And when you try to rebind this function, you might not be aware of why you
bindstatement is not working.It is also very possible (#2) that you need to return someone a bound function from a library you create. Not knowing that you are preventing someone down the road from rebinding that function.
Being able to
binda context inJavaScriptis a very powerful tool.First let's imagine you have an object that uses
this. Because I am unimaginative, I'll create this contrived example:and it works great!
But we have a different use case (for reasons) that doesn't work.
This code doesn't work because calling
mapwill useglobalas yourthis.So you add this creative line to keep
mapalways bound toMyArray:Making this code work as expected...
But what you didn't know is you unknowingly created a bug somewhere else in your application:
So to summarize:
Knowing why this happens is important. Once a function is bound, all future bindings will be ignored. So you must always bind with care.
Or you can do what I do and that is write JavaScript 100% free of
this;)Rethinking JavaScript: The complete elimination and eradication of JavaScript's this.
Of course, no one would write code in
f.bind(x).bind(y)point was that behavior was unpredictable. I was trying to optimize event handlers and I accidentally used bind on bound function and found out that I shouldn't have used bind.thisis powerful, I know lot of people talking about not usingthis, but it is just unnecessary complicated syntax for no real benefit.I am in complete agreement!
I wasn't in disagreement with your article, but the commenter.
Cheers!
Why even suggest eliminating/eradicating the use of
thisthen? Are there any true Javascript experts recommending this? Douglas Crockford, John Resig, somebody at THAT level? John Resig even useswithas deemed necessary (Javascript templates, anyone?), despite everybody parroting howls of derision. There's no sense throwing a good tool such asthisout, like a baby with the bath-water.Why does someone of influence have to recommend it for you to become a follower?
There is no baby/bathwater.
thisis a cancer that needs to be excised.thisin other languages like C# has no problems. Butthisin JavaScript much more complex in comparison for people to reason about. People come from other languages expectingClassto behave in a similar way to their language... and it doesn't.thisis fairly complex to understand completely, which is demonstrated (in a small part) by this article and many many other articles.In JavaScript,
thisis nothing more than an optional argument. So if you need this argument, why not just supply it as an argument?You don't have to ever worry about what
thisis if you never use it ;)Never type
console.log(this)ever again.Ignoring binding arguments, just
.bind(thisArg)can be visualized as following:Here it is clear that the returned function doesn't care about
this.Same thing, without arrow function: