Object creation (Factory) javascript

Asked

Viewed 136 times

3

Hello, I’m having to try to create two objects of the kind person using Factory and in the first attempt 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, that will be a 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 function of factory for raise a 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 list of people[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 got that result

inserir a descrição da imagem aqui

and there are no names of teachers Tom and Peter, or if it is only for one kind of person works all right and if you want create another type of person, returns the value of people who are already created

  • of the one console.log(this.pessoas) there are two indexes, Student and Teacher, now and only you go through them correctly to show the information.

1 answer

2


Your code is correct and you can check it if you do 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 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’re using the ID type of person. That is to say, you are checking person 1, 2, 3 (regardless of whether you are a Student or a Teacher) you are checking the index at this.pessoas reading the id internal that is related to how many such already exist.

Now in case you’re the first registered Professor you’ll be 0 and you’ll be picking up the name of this.pessoas[0].nome and not this.pessoaCriada.nome which is what you’re looking for. If you create it then the first Student will give it again 0 and you’ll get the same amount again. So you’re pointing the flashlight in 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: https://jsfiddle.net/0c72r1uq/

  • 2

    thanks, worked, works perfectly. thanks

  • @You’re welcome, you’re welcome.

  • sometimes small instructions the program does not work as intended but fortunately there was solution

Browser other questions tagged

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