Difference between ways to call a property

4

Reading a book I found the following:

  

Once the properties of the object have been created, they can be accessed by two   Forms:

obj.nombre = "Simon";
var nombre = obj.nombre;

and ...

obj["nombre"] = "Simon";
var nombre = obj["nombre"];
  

These are semantically equivalent. The second method has the advantage that the name of the property is entered as a string, which means that it can be calculated at run time, although using this method prevents some JavaScript engine optimizations from being applied.

What do you mean by 'calculated at runtime' and the other question is the relationship with the javacript engine?

    
asked by Frnk 24.06.2018 в 15:44
source

1 answer

3

I think there are two questions in one, and that even the second one could be broad, but very interesting if you raise it well, but again, it would be a separate issue.

I will just answer your first question.

The code example used by the MDN in the Working with Objects section you can easily see the cases in which it would be convenient to use [] to define the properties of the objects.

Suppose an object that would have, among its properties, a random value. In that case, using brackets you can easily assign that value:

var miObjeto = new Object(),
    cadena = "miCadena",
    aleatorio = Math.random(),
    options= { year: 'numeric', month: '2-digit', day: '2-digit',  hour: '2-digit', minute: '2-digit', second: '2-digit'};

    ahora=new Date().toLocaleDateString('en-GB',options),
    objeto = new Object();

miObjeto.type                 = "Sintaxis con punto";
miObjeto["Fecha de creación"] = "Cadena con espacios y acento";
miObjeto[cadena]              = "String value";
miObjeto[aleatorio]           = "Número Aleatorio";
miObjeto[objeto]              = "Objeto";
miObjeto[""]                  = "Incluso una cadena vacía";
miObjeto[ahora]               = "Valor dinámico (fecha y hora)";

console.log(miObjeto);

As you can see, the fourth property of the object is correctly generated dynamically at runtime.

However, in this way it is not possible.

  • The random value obtained each time at runtime is not created as an object property if you use the Objeto.propiedad syntax. As you can see in the code, you create a property whose name is fixed, not a property with the value of the variable aleatorio obtained in the Random.
  • You can not assign a property like this, used above: Fecha de creación or else error
  • An empty string " " can not be assigned as the property name of an object.
  • An object (such as the dynamically created date object) can not be assigned as the property name. In this case, the property acquires the value Object instead of the value contained within it.

var miObjeto = new Object(),
  cadena = "miCadena",
  aleatorio = Math.random(),

  options = {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit'
  },

  ahora = new Date().toLocaleDateString('en-GB', options),

  objeto = new Object();

miObjeto.type      = "Sintaxis con punto";
//Error ->miObjeto.Fecha decreación    = "Cadena con espacios y acento";
miObjeto.cadena    = "String value";
miObjeto.aleatorio = "Número Aleatorio";
miObjeto.objeto    = "Objeto";
//Error ->miObjeto.""                 = "Incluso una cadena vacía";
miObjeto.ahora     = "Valor aleatorio (fecha y hora actual)";


console.log(miObjeto);
    
answered by 24.06.2018 / 16:40
source