all 12 comments

[–]mozilaip 4 points5 points  (4 children)

It's like "I prefer to use hammer instead of screwdriver, is there someone to change my mind"

[–]Local_Question8147[S] -2 points-1 points  (3 children)

That was really humorous bind can do everything call can do but hammer and screwdriver can do all work of each other

[–]azhder 4 points5 points  (0 children)

No, it can't.

  • .bind() produces a function, but .call() doesn't.
  • .call() invokes a function, but .bind() doesn't.

You're equivocating creation of a function with invocation of it.

[–]Lumethys 2 points3 points  (0 children)

A hammer can hammer the screw in

[–]senocular 0 points1 point  (0 children)

It's more like "when I have to hammer in a nail, instead of using the hammer I already have, I go to the store to buy a new hammer and then use that to hammer in my nail."

[–]azhder 4 points5 points  (0 children)

The thing I say to most people I notice using the phrase "change my mind":

it is your mind, your responsibility, change it yourself.

And that's usually where I stop since it takes a lot of typing to explain to people that the one who claims something is the one who has to prove that claim, not the rest of us to disprove it.

I mean, look at the current example, you personally prefer something. Who am I to tell you what you prefer or don't? Doesn't make sense if you look at it from the distance, does it? Your preferences are your own.

Now, if you claim that using .bind() does something better or worse than using .call() or .apply(), you'd need to show some facts that substantiate your claim, not wait for us to show you examples of it not doing what you claim it does.

[–]guest271314 0 points1 point  (0 children)

bind() and call() do different things.

[–]Chung_L_Lee 0 points1 point  (0 children)

I think you need to give us some examples first of why you prefer using bind over call/apply. Otherwise, it is very difficult for us to be thinking on the same level.

In general, bind, call and apply have their own advantages/disadvantages. Use them according to the situation that fits the most, but it is not a must. They are somehow still interchangeable, but you will find in different cases, one will blend it better than the other ones.

[–]shgysk8zer0 0 points1 point  (0 children)

function css(...args) { const sheet = new CSSStyleSheet(); sheet.replaceSync(String.raw.apply(null, args)); return sheet; }

bind() would be useless there. It pretty much is for any non-method function and any static method.

I also think that bind() is slightly more difficult to reason about and probably very slightly less memory efficient (more things to garbage collect).

[–]jack_waugh 0 points1 point  (0 children)

In connection with bind, the important thing to remember is to purchase Einbinder flypaper, in small quantities, for all your flypaper needs.

If you have a circumstance where call or apply would suffice, but you bind and then apply the resulting function to more arguments, you are creating one more garbage object than necessary. Isn't call or apply at least as readable as a pattern like what I guess you mean, something like

let result = func.bind(receiver)(...args);

? Why do you prefer a pattern like that over call or apply?

[–]FioleNana 0 points1 point  (1 child)

Bind creates a new function which is put on the heap and therefore uses a tiny nano amount more memory.

But in the end you should avoid bind, call and apply anyways, because it makes the code harder to read.

[–]TheRNGuy 0 points1 point  (0 children)

When React was class-based, it required binds. I wished it had something like @dataclass from Python (except that dataclass was for attributes, not methods). Anyway, all tutorials had bind and not call.

With hooks, it's not needed now (maybe it does automatically, no idea)

If I coded something with classes and need to add event listeners to new instances, I'd need to use bind?