what does this syntax mean?

0

I have been trying to know if it is a method or a variable next to a function but I can not understand or find anything, javascript is for example

    var auto = {
    nombre: "Mustang",
    anio: 1965,
    motor: 5.5,
    color: "azul",
    clasico: true,
    es_real: false
  }


  function recorrer_objeto(automovil) {
    console.log(this.nombre);
    console.log(this.anio);
    console.log(this.motor);
    console.log(this.color);
    console.log(this.clasico);
    console.log(this.es_real);
  }
  **auto.mostrarAuto = recorrer_objeto;**
  auto.mostrarAuto();

Would it all show a car variable? or a method? and then it would work out and give the function without necessarily chaining it to auto? (line that I do not understand between ** **; ()

    
asked by Patricio Martin 04.12.2017 в 02:24
source

3 answers

3
  

You should consider reading what you write before posting a question.

In summary:

You created a auto object initializing with its properties, then you created a function called recorrer_objeto that serves to obtain and show by console the properties of the object to which it will be linked.

Also, to the auto object you are defining a method called mostrarAuto assigning it with the function called recorrer_objeto .

  

A method is a function associated with an object, or simply, a method is a property of an object that is a function.

var auto = {
	nombre: "Mustang",
	anio: 1965
}

function recorrer_objeto() {
	console.log(this.nombre);
	console.log(this.anio);
  
	console.log(this.propiedadX);
  console.log(this.propiedadY);
}

console.log('Auto:');

auto.mostrarAuto = recorrer_objeto;
auto.mostrarAuto();

var auto2 = {
	propiedadX: "Mustang",
	propiedadY: 1965
}

console.log('Auto2:');
auto2.mostrarAuto = recorrer_objeto;
auto2.mostrarAuto();

Reference:

answered by 04.12.2017 в 03:38
1

You simply have an object, which serves to keep information, but is more oriented to be as a representation of real objects, what do I mean? I mean that instead of representing, any information without things in common, in an object the information is more related, for example, in an object called auto , you will place the brand, the color, etc.

The function what it does is access the properties of the objects, properties is of the type: key: value , where key is the name of the property and value is its value, as nombre: 'Eduardo' , to access the properties is through the DOT operator, that is, the point . , eg (for example)

miObjeto.propiedad

Then at the end, you create a new property to the object, and you ASSIGN a function, when ASSIGNES , without the parentheses, is why you want that property to be equal to that function, but without necessarily executing it .

Finally, you execute the function.

PS: After having created a new property to the object, in the end, you could have done that, in the same object:

var Obj = {
 copiarFuncion: recorrer_objeto
};

var Obj = {
 nombre: 'Eduardo',
 apellido: 'Campos'
};

function _mostrar (){
 console.log("Su nombre es: " + Obj.nombre, Obj.apellido);
}

Obj.muestrame = _mostrar;
Obj.muestrame();
    
answered by 04.12.2017 в 11:01
-1

This part of the code:

 var auto = {
    nombre: "Mustang",
    anio: 1965,
    motor: 5.5,
    color: "azul",
    clasico: true,
    es_real: false
  }

are just an object in JavaScript

This part of the code is only a function that receives the car parameter that is only referring to the function will receive a value that this case is the value that has the object, for example as in the first console with the

this
are indicating (reference) to the value / property anio of the auto object and with car you are referring to that value.
function recorrer_objeto(automovil) {
    console.log(this.nombre);
    console.log(this.anio);
    console.log(this.motor);
    console.log(this.color);
    console.log(this.clasico);
    console.log(this.es_real);
  }

This part I understand but my question is: is it all the code or is it incomplete?

  **auto.mostrarAuto = recorrer_objeto;**
  auto.mostrarAuto();

But I hope that you understand the code with my explanation.

    
answered by 04.12.2017 в 03:30