Good, this is not jQuery, it's pure JavaScript .
["Domingo", "Segunda", "Terça", "Quarta", "Quinta", "Sexta", "Sábado"][new Date().getDay()]
This is a literal of an array and access to one of your elements. Let's do what Jack says, let's break it down:
["Domingo", "Segunda", "Terça", "Quarta", "Quinta", "Sexta", "Sábado"]
This part is the literal that defines the array . It has 7 elements and all are strings . Each is placed in an index starting at 0, so the last one will be 6.
If you wanted to assign this to a variable it might look like this:
var array = ["Domingo", "Segunda", "Terça", "Quarta", "Quinta", "Sexta", "Sábado"];
If you want to access element 0 of this array it could be:
array[0]
If I do not know at the time of creating the code what is the index number I could use a variable that will have its value come from somewhere:
array[x]
Or you could use a expression , after all a variable is still an expression:
array[new Date().getDay()]
It is known that getDay()
returns a number of 0 to 6, according to the day of the week. This is exactly what we need as the value of the array index we create. The function is applied to an object that has just been created, and this object is today's date. It is the same principle of the variable replaced by a literal. Both are appropriate expressions for the context. See another way:
var data = new Date();
var diaSemana = data.getDay();
console.log(diaSemana);
Since we do not need the variables, we use:
console.log(new Date().getDay());
But think carefully, why create a variable? Variables are storage locations of memory values with a name. If I need to "calculate" a value, but I do not need to store it anywhere there, why would I need a variable? Without the variable, I can just use the literal, so in place of the name array
I can use the value that defines this array , like this:
var diaDaSemana = new Date().getDay();
return ["Domingo", "Segunda", "Terça", "Quarta", "Quinta", "Sexta", "Sábado"][diaDaSemana];
But even this variable diaDaSemana
is not required, I can use a literal, I can use the direct value:
return ["Domingo", "Segunda", "Terça", "Quarta", "Quinta", "Sexta", "Sábado"][new Date().getDay()];
In case of that response this value is used to return in the function. If the variable is just a name for a value, I can use the value directly in place in the name. see if you can better understand the form with unnecessary variables:
function diaDaSemanaHoje() {
var array = ["Domingo", "Segunda", "Terça", "Quarta", "Quinta", "Sexta", "Sábado"];
var hoje = new Date();
var diaDaSemanaHoje = hoje.getDay();
var textoDiaDaSemanaHoje = array[diaDaSemanaHoje];
return textoDiaDaSemanaHoje;
}
console.log("Hoje é " + diaDaSemanaHoje());
Is it magic?
There is nothing magical about this. It is the normal of language. He did not need anything special. Within the language specification , what is coherent, linear, symmetric, is to work like this.
I remember the only mainstream language that did not allow this was PHP and today allows.
The ideal to program is to understand the reason for each space, each comma in the code. It is only when you master this that you have learned to really program. You know how to do anything. Before and get to this point is just repeating cake recipes already made before by other people. Even when the recipe is so short when using a variable.
In everything you write in the code you need to know why you're doing it. You need to understand the role of each token of the code. And how it can be replaced according to the rules of language, which incredible as it may seem, in the basics, vary less than what people imagine.
Programming is to put together a lot of Lego piece. You have to know everything about each piece.
Good to have asked:)