Creating javascript (factory) objects

3
Hello, I'm trying to create two objects of type person using Factory and on the first try I create the first element and the second attempt instead of creating the second element creates a new element , but with the same characteristics as the first element

Classe Pessoa , which will be super classe

function Pessoa(id, nome) {
  this.id = id;
  this.nome = nome;
}

Classe Aluno extends Pessoa

function Aluno(id, nome) {
  Pessoa.call(this, id, nome);
}

Classe Professor extends Pessoa

function Professor(id, nome) {
  Pessoa.call(this, id, nome);
}

Using the factory function to create student and teacher

function Factory() {
    var idAluno = 0;
    var idProfessor = 0;

  this.criarPessoa = function(tipo, nome) {
    var pessoa = new Pessoa();
    switch (tipo) {
      case "1":
        pessoa = new Aluno(idAluno++, nome);
        break;
      case "2":
        pessoa = new Professor(idProfessor++, nome);
        break;
    }
    return pessoa;
  }
}

Classe Escola with a people list [ alunos e professores ]

function Escola(id) {
  this.pessoas = [];
  this.factory = new Factory();
  this.pessoaCriada = null;

  this.criarProfessorOuAluno = function(tipo,nome) {

    if (tipo!== null) {
      this.pessoaCriada = this.factory.criarPessoa(tipo,nome);

      this.pessoas.push(this.pessoaCriada);

      console.log("\nID: "+this.pessoas[this.pessoaCriada.id].id+
      "\nNome: "+this.pessoas[this.pessoaCriada.id].nome);
    } else {
      console.log("não pode ser vazio");
    }
  }
}

test in cmd, node app.js

var escola = new Escola(1);
escola.criarProfessorOuAluno("1","Jonh"); // 
escola.criarProfessorOuAluno("1","Bob"); // 
escola.criarProfessorOuAluno("1","Jerry"); // 
escola.criarProfessorOuAluno("2","Tom"); // 
escola.criarProfessorOuAluno("2","Peter"); // 

and I get this result

and the teacher names Tom and Peter do not appear, or if it's just for one kind of person works fine and if you want to create another type of person, return the value of people already created

    
asked by anonymous 28.05.2016 / 16:41

1 answer

2

Your code is correct and you can check this if you make console.log(escola.pessoas); that will give

[{
    "id": 0,
    "nome": "Jonh"
}, {
    "id": 1,
    "nome": "Bob"
}, {
    "id": 2,
    "nome": "Jerry"
}, {
    "id": 0,
    "nome": "Tom"
}, {
    "id": 1,
    "nome": "Peter"
}]

What is not correct is how you are checking with the console for the data you are creating. When you use

console.log(
    "\nID: " + this.pessoas[this.pessoaCriada.id].id +
    "\nNome: " + this.pessoas[this.pessoaCriada.id].nome
);

You are using ID of the type of person. In other words, instead of checking for person 1, 2, 3 (regardless of whether you are a student or a teacher) you are checking the index as% with% reading the% with% of internal% that is related to the number of that type already exists.

Now if you are the first Registered Teacher you will be this.pessoas and you will be looking for the name of id and not 0 which is what you are looking for. If you create then the first Student will give this.pessoas[0].nome again and you will receive the same value again. You are therefore pointing the flashlight at the wrong place.

I think you want to use

var ultimo = this.pessoas.length - 1;
console.log(
    "\nID: " + this.pessoas[ultimo].id +
    " Nome: " + this.pessoas[ultimo].nome
);

jsFiddle: link

    
28.05.2016 / 17:23