Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function passed as reference inside setTimeout

Tags:

javascript

I am studying a bit Angular docs and there's an assignment I do not really get.

setTimeout(() => this.seconds = () => this.timerComponent.seconds, 0);

Does it set a reference of this.timerComponent.seconds to this.seconds? Given that both seconds and timerComponent.seconds are methods, is it equivalent to the following?

 setTimeout(() => { this.seconds = this.timerComponent.seconds }, 0);
like image 976
jeremyTob Avatar asked Dec 21 '25 08:12

jeremyTob


1 Answers

It's equivalent to

setTimeout(() => { return this.seconds = (() => { return this.timerComponent.seconds; }); }, 0);

It passes a function to setTimeout that, when called, assigns a function to this.seconds that, when called, returns the current value of this.timerComponent.seconds.

like image 94
melpomene Avatar answered Dec 23 '25 21:12

melpomene



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!