Javascript with Object Vectors

4
var Carro = function(){
    var marca;
    var modelo;
    //this.setMarca = setMarca;
    this.setMarca = function(_marca){
        this.marca = _marca;
    }

    this.setModelo = function(_modelo){
        this.modelo = _modelo;
    }
    this.getMarca = function() {
        return this.marca;
    }
    this.getModelo = function(){
        return this.modelo;
    }

}

var carro = new Carro();
var marca = [];
var modelo = [];
var i;
var string = " ";
carro.setMarca("Volkswagen");   
carro.setModelo("Camaro");  
alert(carro.getMarca() + " " + carro.getModelo());

for(i=0;i<5;i++){
    marca[i] = carro.getMarca() + " " + i;
    modelo[i] = carro.getModelo() + " " + i; 
    string = string.concat(marca[i] + "\n");        
    string = string.concat(modelo[i] + "\n");   
}
alert(string);

I have a car class, it has getters and setters for the tag and the template.

How can I save the data in a single vector? I have separated into two vectors like this:

var marca = [];
var modelo = [];

Is there another way to merge both data into a single vector? for example:

var car = [];
car|0|->marca = carro.getMarca();
car|0|->modelo = carro.getModelo();
    
asked by anonymous 02.03.2015 / 00:16

2 answers

4

As you already have a class Carro , all you have to do is instantiate it and store the items in your vector:

var car = [];
car[0] = new Carro(); // Novo carro na posição 1
car[1] = new Carro(); // Novo carro na posição 2
car.push(new Carro()); // Novo carro na última posição

Then you can access them as you want:

car[0].setMarca("xxx");
car[2].getModelo();

Some other things I noticed in your code:

  • var marca; var modelo; at the start of your constructor is unnecessary - you are simply creating two local variables, which are not accessed anywhere. You could even use them to do encapsulation, like this:

    var Carro = function(){
        var marca; // Privado, pois somente é acessível dentro do código do construtor
        this.setMarca = function(_marca){
            marca = _marca; // Pode acessar marca, pois está num closure
        }
        this.getMarca = function() {
            return marca; // Pode acessar marca, pois está num closure
        }
    }
    
  • If you really want to use getters and setters , but you do not want to encapsulate the data in closure (as in the example above), it is more efficient to place these methods in prototype . So you avoid making a copy of them for each Carro object, with a single copy shared by all objects:

    function Carro() { }
    Carro.prototype.setMarca = function(_marca) {
        this.marca = _marca;
    };
    Carro.prototype.getMarca = function() {
        return this.marca;
    }
    
02.03.2015 / 00:42
2

Yes, each vector position can be an object:

var marcaModelo = [];
for ( var i = 0, ii = carros.length; i < ii; i++ ) {
    marcaModelo.push({
        marca: carros.getMarca(),
        modelo: carros.getModelo()
    });
}

And you can access the data in the vector as follows:

console.log(marcaModelo[0].marca, marcaModelo[0].modelo);

You will probably have duplicate data (objects with the same make and model in more than one vector index). It is an exercise to navigate the vector and only perform the insertion without creating duplicity.

    
02.03.2015 / 00:30