Fun Stuff > CLIKC
Need help (lots) with Javascript
Sorflakne:
So wait, ; are NOT required in javascript? But my scripts seem to break if I forget one...unless I'm imagining things.
snalin:
If you're writing on a site, they might be using an extra static checker. If you are doing several things on one line (like inside a for loop), they are necessary, for the parser to tell one statement from the next. Otherwise, well, not really, but you should use them anyways. They add clarity to your code (yes I DID mean to end the statement there, I didn't just accidentally press enter at one point), and as most languages with syntax similar to JS has mandatory semicolons, most people who will read your code will be used to them.
Stybar:
As Snalin said, semicolons may not be neccesary in a language, but it's considered good practice to do so. Same as naming your variables properly ("counter1" doesn't make much sense, "clickCounter" does), correct writing of variables (capital letters!), proper decleration of your variables,...
Learning to code is one thing, but to code well is something else.
snalin:
Problem is, of course, that what nice code should look like... not set in stone. As many opinions as programmers. Don't get me started on tab vs. spaces discussions, or inline ifs or... jeez. Consistency is always a virtue, though. Find something that looks good, and stick to it. If you're working on an open source project or with a company, find out what the current style of the project is, and use that.
Sorflakne:
Ah ok. Gonna keep the ;'s then, since I'm still getting used to the code.
Ok, here's another thing I've run into, and it took some hair pulling and whatnot to figure out. Here's code for two buttons:
--- Code: ---<button onclick="timedCount()">Start count</button>
<input type="button" value="Start count" onClick="timedCount()" />
--- End code ---
Both point to the same function when placed in the same block of code, but for some reason, only the top one works (this is for a basic timer; text of the function is the same for both attempts). The second one creates a button, but its nonfunctioning. I've had this happen several other times, and each time when I went back and changed the code from the second format to the first's, then it works fine. What causes one to work and the other to not?
Edit: Ok I just did another timer code for practice, and now the second button code works, but the first one doesn't. Wtf.
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version