Javascript with Object Vectors

Asked

Viewed 2,071 times

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 type class, in it have getters and setters for make and model.

Is it possible to store the data in a single vector? I separated it into two vectors so:

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

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

var car = [];
car|0|->marca = carro.getMarca();
car|0|->modelo = carro.getModelo();

2 answers

4


As you already own a class Carro, all you need to do is instantiate it and save 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 wish:

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

Some other things I’ve noticed in your code:

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

    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 does not want to encapsulate the data in a closure (as in the example above), it is more efficient to put these methods in the prototype. So you avoid making a copy of them for each object Carro, existing a single copy shared by all objects:

    function Carro() { }
    Carro.prototype.setMarca = function(_marca) {
        this.marca = _marca;
    };
    Carro.prototype.getMarca = function() {
        return this.marca;
    }
    

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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.