0

I'm having 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

class Person

function Person(id, name) {
  this.id = id;
  this.name = name;
}

class Student extends Person

function Student(id, name) {
  Person.call(this, id, name);
}

class Teacher extends Person

function Teacher(id, name) {
  Person.call(this, id, name);
}

using function factory to create student and teacher

function Factory() {
  this.createPerson = function(type, name) {
    var person;
    var idStudent = 0;
    var idTeacher = 0;

    switch (type) {
      case "1":
        person = new Student(idStudent++, name);
        break;
      case "2":
        person = new Teacher(idTeacher++, name);
        break;
    }
    return person;
  }
}

class School has an array of person

function School(id) {
  this.persons = [];
  this.factory = new Factory();
  this.personCreate = null;

  this.createStudentAndTeacher = function() {
    var type = prompt("Choose ? \n\n[1-Student | 2-Teacher ]");
    var name = prompt("Write name?");

    if (type !== null) {
      this.personCreate = this.factory.createPerson(type,name);
      this.persons.push(this.personCreate);
    } else {
      alert("need to choose");
    }
  }
}

created by default

var s = new School(1);

var btn = document.createElement('button');
btn.value = "create";
btn.onclick = function(){
  s.createStudentAndTeacher();
}

my doubt is when I create the Student object with name "John", return student.id = 0 and student.name = John but when I create the new Student object with name "Elisa", return the same information student.id = 0 and student.name = John and in fact, should return student.id = 1 and student.name = Elisa and if I create new Teacher object with name "Jerry", return the same information student.id = 0 and student.name = John and in fact, should return teacher.id = 0 and teacher.name = Jerry

what I´m doing wrong?

1
  • Are you caching School? Commented May 27, 2016 at 10:41

2 Answers 2

2

The code has a bug. In the Factory definition, change this

function Factory() {
  this.createPerson = function(type, name) {
    var person;
    var idStudent = 0;
    var idTeacher = 0;

to this

function Factory() {
  var idStudent = 0;
  var idTeacher = 0;
  this.createPerson = function(type, name) {
    var person;

then use the console, run these lines.

var s = new School();
s.createStudentAndTeacher(); // john
s.createStudentAndTeacher(); // bob
s.persons[0] // id 0:john
s.persons[1] // id 1:bob
Sign up to request clarification or add additional context in comments.

3 Comments

I run your code before porting the answer and it worked.
ok, if I call the btn.onclick = function(){ s.createStudentAndTeacher(); } does the same as above
while debugging try one step at a time. Copy the code you show us here to a new file. Do the changes we propose. Open the console and run my lines. If does not work, try tha same on other browsers. If it works, THEN try to check that code and DIF it with the new one.
2

Your createPerson is redeclaring idStudent and idTeacher (and resetting them to 0). Try moving that code out of that block.

1 Comment

I tried to redeclaring var idStudent and var idTeacher to this.idSudent and this.idTeacher but still the same

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.