I do not know what this syntax is

4

Someone can help me with this structure, I do not know what it is, nor how it works.

var variavel = {
    'teste1':'1',
    'teste2':'2',
    'teste3':'3'
}
    
asked by anonymous 30.06.2016 / 22:00

3 answers

6

This is how to define an object. The object's members are on the left side of : , the members' values are on the right side.

var variavel = {
    'teste1':'1',
    'teste2':'2',
    'teste3':'3'
};
console.log(variavel.teste1); //acessando o membro
console.log(variavel['teste2']); //sintaxes diferentes, mas mesmo resultado
var variavel2 = { //é mais comum fazer desta forma, ainda que funcione igual
    teste1:'1',
    teste2:'2',
    teste3:'3'
};
console.log(variavel2.teste1); //sintaxes diferentes, mas mesmo resultado
console.log(variavel2['teste2']);

Note that keys are used to define objects, that is, key pairs (left side) and values (right side). If you use brackets, you are defining a data string.

var variavel3 = ['1', '2', '3'];
console.log(variavel3[0]);
Depending on the use you can only do double quotes, this is the case of JSON , although some interpreters accept it as simple.     
30.06.2016 / 22:05
5

This is the creation of an object with the properties teste1 , teste2 and teste3 . What has on the left side is the identifier of the property, the right side is the value.

It's the same thing as making the code below.

var variavel = {
    teste1: '1',
    teste2: '2',
    teste3: '3'
};

console.log(variavel.teste1);
    
30.06.2016 / 22:03
1

This is an object. The structure follows the standard attribute and its value. This value can be String , boolean , int , function() , another object. Here is an example:

var objeto = {
    atributoUm: 'String',
    atributoDois: 10, // inteiro
    atributoTrês: true, //boolean,
    atributoQuatro: [], //array
    atributoCinco: {}, // outro objeto
    atributoSeis: function () {} //function,
    atributoSete: funcao.metodo() //método
}

To access any attribute, simply call your object point attribute. Example:

objeto.atributoUm // acessa esse específico atributo deste objeto
    
25.11.2016 / 14:24