Obtain properties of an object which belongs to another object

4

Greetings.

I've been trying to know how to get the properties of an object that is at the same time inside another object. This comes as a result of a programming book.

The code is as follows.

1st we have the object:

    function Concesionario(cod_oficina_in,ciudad_in,responsabilidad_in){
    this.cod_oficina = cod_oficina_in;
    this.ciudad = ciudad_in;
    this.responsabilidad = responsabilidad_in;
    }


   function Coche(marca_in,modelo_in,anyo_in,concesionario_in){
   this.marca = marca_in;
   this.modelo = modelo_in;
   this.anyo = anyo_in;
   this.concesionario = concesionario_in;
   }

  var concesionario_atocha = new Concesionario('281','Madrid','Pedro Bravo');

  var mi_coche = new Coche('Citroen','C4','2010',concesionario_atocha);

By means of a for..in I can extract the properties of an object in this case my_car

My question is:

How can I show or store the properties of the concession-holder object to which it belongs as property of the my_car object? a for..in, for, while loop or through a function ..?

 for( indice in mi_coche){
      document.write(....propiedades.concesionario_atocha ... ? )
      }

Thank you for your attention. It's already a question that I want to get out of my head since I've tried everything, nested for..in loop, nested for, while, use of arrays .. no idea.

    
asked by Sin Nombre 22.02.2017 в 17:06
source

2 answers

2

Your objects are actually class instances with public properties, so when you iterate over them you will also pick up prototype methods that would not come in a flat object.

However, you can iterate over the conditional by:

for(var key in mi_coche.concesionario) {
    console.log(mi_coche.concesionario[key]);
}

As I said before, probably doing that will give you more keys than those declared in the Dealer constructor. To leave them out, you should do:

for(var key in mi_coche.concesionario) {
    if(mi_coche.concesionario.hasOwnProperty(key)) {
       console.log(mi_coche.concesionario[key]);
    }
}
    
answered by 22.02.2017 в 18:13
1

You are confusing things, the reserved word new is used to create a new object by reference to the constructor of a class, therefore it is not used for functions (object actions)

In your code you are using the reserved word new for a function.

var concesionario_atocha = new Concesionario('281','Madrid','Pedro Bravo');

the functions come to be the actions of the objects, a good practice is to name the functions with verbs, such as driving, running, walking, swimming, etc.

your code:

function Concesionario(cod_oficina_in,ciudad_in,responsabilidad_in)
    this.cod_oficina = cod_oficina_in;
    this.ciudad = ciudad_in;
    this.responsabilidad = responsabilidad_in;
    }

the above code is a function, you can not instantiate objects of a function, we must return it Class, as follows:

 Class Concesionario{
   int cod_oficina ;
   String ciudad;  
   String responsabidad;



public Concesionario(cod_oficina_in,ciudad_in,responsabilidad_in){
     this.cod_oficina = cod_oficina_in;
     this.ciudad = ciudad_in;
     this.responsabilidad = responsabilidad_in;
    }
  }

now answering your question: you can access by creating get functions

por ejemplo:

public int getCodOficina(){
return cod_oficina;
}
    
answered by 22.02.2017 в 17:35