Event listener question
https://codepen.io/Adhurim-Gashi/pen/eYqZzRo
- Hey guys, I am building a little calculator project with a tutorial for the (js) so in my js code whenever i click a button i get the console.log of the characters or numbers inside the calculator, and can someone decipher how those lines of code for me, cause i don't understand it that much, thanks.
10 Replies
what do you want to "decypher"?
* I personally would consider defining a value for each button and sending that rather than relying on the button text
thanks chris
the last point is very interesting and important
and i would even go further and say to use a form instead of click handlers on all buttons
that said, You could just do this
or if you must:
setting up an init and then wrapping the buttonClick call in a separate function is a whole bunch of useless overhead
Did you write this code, or did you get it somewhere?
it is useless if you dont use it on a
window.addEventListener('ready', ...)
or other method of detecting that the browser is ready
which, now-a-days, can mostly be mitigated by using the defer
attribute in the script tagthe init() function is just called in the original example too, so it doesn't really do anything
well, there is actually 1 useful thing from it: no global variables and constants
if you put the code outside the function, all the variables and constants are global automatically, because they are in the top scope
but this creates a global
init
which is equally bad
also, it is missing 'use strict';
hmm, I suppose, yeah
the most useful would be a ready event handler or an anonymous function call, instead of a
main();
à la c