The Questions of if/else statements and other alike codes
Hi, currently I am using this code to call an if/else statement, to add and/or remove the class ".hide" whenever the different tables are clicked on. This code is working all fine, no trouble with it at all.
Example 1
$(document).ready(function() {
$('#Year_Table').click(function() {
if ($('#Month_Table').hasClass("hide")) {
$('#Month_Table').removeClass('hide');
} else {
$('#Month_Table').addClass('hide');
$('#Day_Table').addClass('hide');
}
});
});
But what I am wondering about, is if there is anyway to make the code shorter? but with the same outcome. That the class is add or removed.
I had another question a few days ago, where a guy shortened my if/else statement to this code below. Thank you
Example 2
$( document ).ready(function(){
var games_month = 0;
var month_games = "Games";
$("#Games_Month").html(games_month);
$('#January').click(function(){
$('#Games_Month').html(function(_, html) {
return html === month_games ? games_month : month_games;
});
});
The shortened code works perfectly too, but gives me a couple of question marks. Now, what I would like to know is:
1: What is this type of code that was shortened for me? I mean, what is it called?
I have been told it might be a callback function.
2: What is different from a standard if/else statement and the shortened code?
is it the same, just cleaned up? or is there any important difference?
3: What does the different parts of the shortened code mean? what do they do?
To me it just seem like another kind of an if/else statement?
4: is there any way to make such a code with classes instead of variables?
Simply instead of changing the variable, I would like to add or remove the class ".hide" and is it possible to add and remove several classes within this function?
5: Is there any other ways to code an if/else statement or code which gives the same result?
I am very new at using both javascript and jQuery, but I'm trying to learn as much as possible. I appreciate all the help I can get to understand all this, everything will help me getting further into my knowlegde of coding.
Thank you very much.
Aucun commentaire:
Enregistrer un commentaire