#how can I make this code cleaner ?

21 messages · Page 1 of 1 (latest)

bitter hound
#

Hello, How do I make this code cleaner ?

I have main js file and this dom manipulation js file which I export to main js file.

Should I declare dom variables inside functions they are used in instead of putting them on top of file?

Is it correct to have init() function, like in my code, which holds other functions and then export it to main js file and call it there?

All recommendations are welcome, thanks !

mortal questBOT
#

See #faq to learn how to properly share code on Discord: #faq message

agile folio
#

Should I declare dom variables inside functions they are used in instead of putting them on top of file?

this is fine, but should be at the top of each file that has methods using those elements.

exporting the init function is kinda nasty. your lack of semicolons is also nasty. 😉

bitter hound
agile folio
#

guess I'm not understanding why you need an init function

#

this is incredibly hard to read. not having semicolons makes it even worse. you do you, though!

bitter hound
#

I could just add if else if there to make it more readable but its more code, and i used init so when i import it to main file i just call init() instead of each individual function

agile folio
#

just call those functions in the root of the imported script? don't make the main file call anything?

bitter hound
agile folio
#

when you import a script, everything in it is executed

#

so don't export init and call that... just call those functions within the script itself.

#

only export what you actually need in another context/scope

mortal questBOT
#

See #faq to learn how to properly share code on Discord: #faq message

bitter hound
bitter hound
bitter hound
# agile folio No.

I read everything but I still dont understand what I should do, could you explain me in code?
Id still want to have 2 js files to seperate dom manipulation and other codes so its less busy than keeping it all in one file, unless that is wrong too

agile folio
#

separating by DOM manipulation and "other" is a little strange, but there's nothing wrong with it. from the link, you should be using one of the following in your <head> section.

<script type="module" src="dom.js">
<script defer src="dom.js">
bitter hound
agile folio
#

I don't understand the question but yes you can use modules even if you don't export.