Well, the use of the colon is basically used in objects:
var frutas = {
"banana":{
cor: "yellow"
}
}
With the object you simply "navigate" between levels of the keys:
frutas.banana.cor; // yellow
So whenever a key is used on an object, a value is assigned to it, by means of the two points :
, instead of the =
, used for general variables.
In your example I think a }
key is missing, but it must have been the collage ...
function paciente(nome, idade, altura) {
var clazz = {
imprime: function() {
alert("nome: " + nome + ", idade: " + idade);
}
}
return clazz;
}
You have a function, which has an object, this object receives a imprime
key, it receives a function that returns a concatenation of the first two arguments of the function, that is:
paciente('João', 35, "1,80m").imprime() // João, 35
There are other uses, for example in conditional variables:
var cor = arguments.length > 0 ? arguments[0] : "black";
In the example above, assuming this code is inside a function, you are saying: If the number of arguments is greater than zero the variable "color" will be equal to the first argument, otherwise it will be the same to "black" .
The "snag" / "else" is represented by the colon (colon).
There are also labels
, implemented with EcmaScript , look:
var i, j;
loop1:
for (i = 0; i < 3; i++) { // Primeira declaração rotulada "loop1"
loop2:
for (j = 0; j < 3; j++) { // Segunda declaração rotulada "loop2"
if (i === 1 && j === 1) {
continue loop1;
}
console.log("i = " + i + ", j = " + j);
}
}
You can use label
to identify a loop , and then use break or #
I left the latter option only for curiosity level I almost never see subjects about these labels
, I also do not use too often, but they are very interesting, I recommend you to see the links.