Using ES6 Functions | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Using ES6 Functions

Hey everyone! I wanted to make my entire code with ES6 features but I failed at some point. So, my questions are: - How to make the setInterval() as an arrow function, without having to come up with a name for the function itself? - Sometimes, when I make an arrow function with a variable declaration, for example: const clk = ()=>{}; I get an Uncaught reference error : Cannon access clk before initialisation. What does that even mean? https://code.sololearn.com/WVEdzbW3W5Ot/?ref=app

3rd Sep 2020, 6:58 PM
Vasilis Karapas
Vasilis Karapas - avatar
7 Answers
+ 4
Hi Vasilis Karapas, I found this very cool article. (I hope it helps you) https://googlechrome.github.io/samples/arrows-es6/ even setInterval es6 version is covered there.
3rd Sep 2020, 7:01 PM
Rohit Kh
Rohit Kh - avatar
+ 4
let count = setInterval(showTime, 1000); let count = setInterval(() => showTime(), 1000); something like this?
3rd Sep 2020, 7:10 PM
Rohit Kh
Rohit Kh - avatar
+ 4
Like this as well, window.onload=()=>{ console.log("hi"); setInterval(this.onload,1000) }
3rd Sep 2020, 7:33 PM
Abhay
Abhay - avatar
+ 3
Code works perfectly by the way. I just wanted to make some minor adjustments, to the onload function and the setInterval() to make it ES6 style.
3rd Sep 2020, 7:00 PM
Vasilis Karapas
Vasilis Karapas - avatar
+ 2
RKK It's a nice start. It works nicely. However, I want to link this to the window.onload function by making the window.onload ES6 style, also. So far, I was able to get the setInterval() work only by naming the window.onload function and writing it as a normal function, not an arrow one.
3rd Sep 2020, 7:22 PM
Vasilis Karapas
Vasilis Karapas - avatar
+ 2
RKK Thanks for the article, I'll have a look.
3rd Sep 2020, 7:22 PM
Vasilis Karapas
Vasilis Karapas - avatar
+ 2
Abhay Wow, I didn't know I can do that. That's awesome!
3rd Sep 2020, 7:44 PM
Vasilis Karapas
Vasilis Karapas - avatar