I do not have much experience with Javascript’s prototype and oriented objects features — I just started to use them a couple of weeks ago. Nevertheless, based on what I know, your story seems wrong. You use bind on a function that does not use this — how can this be useful? Perhaps, you meant to use bind(null, ...args) , which passes the arguments ...args .
At the least in my environment, your code returns many undefined values or exceptions. Let’s look at the statement
setTimeout(run.bind('Aquaman'), 1000)After 1 second, the function run = name => console.log(name) is executed, but no argument name is passed. The value of this is ‘Aquaman’, but it’s not used. So, the returned value is undefined . Perhaps you meant:
setTimeout(run.bind(null, 'Aquaman'), 1000)Let’s look at
setTimeout(run('Superman'), 1000);This throws an exception because the first argument to setTimeout must be a function and run('Superman') is not a function.
Have you tested your code? Anyway, I think it’s a good idea to experiment with setTimeout, especially with a 0 second delay. What do you think will happen with this snippet?
var i = 0;
let run = name => console.log(name);
setTimeout(run.bind(null, 'Aquaman'), 0);
while (i < 10000)
{
i++;
run('Batman' + i);
}When do we get out of the current flow and seek the next event in the message loop? It seems that the while loop is executed first and then we execute run.bind(null, 'Aquaman') in the message loop. What is the general rule? The answer is given here. It basically says that the entire script is executed before the callbacks, because the call stack contains the entire script and it is executed before the callbacks.
