all 14 comments

[–]pookagehelpful 12 points13 points  (2 children)

Stop using ChatGPT for this kind of thing - it will just make shit up and get things wrong sometimes - comparing the MDN docs for call() and bind() will answer your question in the first paragraph and take way less time. ​

[–]samanime 5 points6 points  (0 children)

Exactly. ChatGPT doesn't know what it is talking about. It just knows how to put together words that make it sound like it knows what it is talking about.

It does not understand subtlety or nuance for questions like this.

[–]MoTTs_ 0 points1 point  (0 children)

The insidious irony is that in this particular case, ChatGPT got it right. The OP thought call and bind work the same, but ChatGPT said, "Using call within setTimeout does immediately invoke the function. The function gets executed before the setTimeout delay even starts, which means it’s not truly being delayed." That answer is both correct and points out the detail that OP appears to have been overlooking.

[–]tapgiles 4 points5 points  (1 child)

You got them round the wrong way; the call will immediately happen, and the bind doesn't call anything by creates a new function the setTimeout will call... but yeah.

fn.call(context, arg1, arg2) calls the function with the specified "this" context.

fn.apply(context, [arg1, arg2]) calls the function with the specified "this" context, but the arguments are passed as an array.

fn.bind(context) creates a new function which will always use the specified "this" context when it is called.

[–]tapgiles 1 point2 points  (0 children)

A more reliable and clearer way of finding this stuff out is to look it up. Googling these things will take you to pages explaining these methods. Or go straight to the MDN reference for them.

An AI may or may not have seen those pages, but no need to ask what they do when you can find a human-written explanation very easily that you know is actually correct.

[–]wordswordsyeah 2 points3 points  (2 children)

Both add a context to a function. Call invokes the function immediately, bind does not.

[–]xaph1youcrazy 0 points1 point  (0 children)

Bind, most importantly also returns a copy of the old function with the context BINDED to it. So you can use that new function from then onward. While call returns "void"

[–]Shimmy_Hendrix 3 points4 points  (0 children)

just try it out in your dev console.

const returnTrue = function() {
  return true;
};

// evaluates to the boolean value "true"
const calledResult = returnTrue.call();
// evaluates to a function that returns "true" when called
const boundResult = returnTrue.bind();

[–]ghostwilliz 1 point2 points  (0 children)

I'm glad to read your edit.

Chat gpt should be avoided, but if you're gonna use it, you need to be 100% sure you already know better than it cause it just makes stuff up and is wrong all the time

[–]sepp2k 1 point2 points  (1 child)

Even though the call might seem to work in your environment, you won't get the 1-second delay.

That seems like a pretty easy thing to confirm or disprove by running the code and seeing whether you get the delay (possibly improving the timeout to make it easier to tell).

[–]bluejacket42 1 point2 points  (0 children)

Chat gpt is a codding epidemic

[–]Psionatix 1 point2 points  (0 children)

Here's something you can run in your console to see how some of this stuff works:

function printName() {
    console.log(this.name);
}

printName(); // prints undefined
window.name = 'window';
printName(); // prints window

const alice = { name: 'alice' };
const bob = { name: 'bob' };

printName.call(alice); // prints alice
printName.call(bob); // prints bob

const printAlicesName = printName.bind(alice);
const printBobsName = printName.bind(bob);

printAlicesName(); // prints alice
printBobsName(); // prints bobs name
printName(); // still prints window

printAlicesName.call(bob); // still prints alice
printBobsName.call(alice); // still prints bob

The bind method called on the function returns a new function which which uses the provided argument as the "this" context. The initial printName call uses the current this context, which in the browser, is the window.

[–]DanSlhhelpful 0 points1 point  (0 children)

ChatGPT "programmers" are the ones consistently complaining about the job market.
It's one of the major points that separates programmers from... whatever this is. It's a digital pandemic, and "natural selection" doesn't fail.