Increase integer to Javascript object

2

I am practicing with objects and I have this method curarPersonas where it is supposed to add one to the variable personasCuradas each time it enters the function; but it only adds it once and in the second it does not even show it. I wonder if there is any way to increase that value.

function medicoInternas(nombreDoctor, personasCuradas, especialidadDoctor) {
    this.nombre = nombreDoctor;
    this.curadas = personasCuradas;
    this.especialidad = especialidadDoctor;

    this.curarPersona = function() {

        this.curadas = parseInt(this.curadas);
        this.curadas++;
        alert("Persona curada, total de personas curadas " + this.curadas);
    }

    this.mostrarDatos = function() {
        window.alert("El nombre del medico es: " + this.nombre + " su especialidad es: " + this.especialidad + " y lleva " + this.curadas + " personas curadas");
    }
}

function arranca(valor) {

  var medico = new medicoInternas(aquiVaUnNombre", 0, "AquiUnaEsp");

  if (valor == "curar"){
      medico.curarPersona();
    }
  else {
        medico.mostrarDatos();
    }   
}

Thank you.

    
asked by Marco Diaz 19.03.2017 в 15:42
source

1 answer

0

Every time you call arrancar a new instance of medicoInternas is being created, so if you call curarPersonas it will increase from 0 to 1 and when you finish running arrancar the object medico will be destroyed .

You must instantiate medicoInternas out of the function arrancar :

var medico = new MedicoInternas("Leslie", 0, "Cardiología");

function arrancar (valor) {
  if (valor === 'curar') {
    medico.curarPersonas();
  } else {
    medico.mostrarDatos();
  }
}
  

It's better to add the instance methods in the prototype

Every function in JavaScript has a special object, called prototype that inherits from Object . In this special object there are all the properties and functions that every instance of that function will have.

Adding the functions in the prototype of a base function means gaining performance and good practices. When you create functions via this you are doing that a new function is created for each instance of the base function; This is very bad as it degrades performance in complex applications. The correct thing is to add functions to the prototype:

function MedicoInternas (data) {
  this.nombre = data.nombre;
  this.curadas = data.personasCuradas;
  this.especialidad = data.especialidadDoctor;
}

MedicoInternas.prototype.curarPersona = function () {
  this.curadas++;
  console.info('Persona curada');
};

MedicoInternas.prototype.mostrarDatos = function () {
  console.info('Nombre:', this.nombre);
  console.info('Personas curadas:', this.personasCuradas);
  console.info('Especialidad:', this.especialidad);
}

This way each instance of MedicoInternas will share the same functions . This is right and optimal and should be done like this.

Final code

function MedicoInternas (data) {
  this.nombre = data.nombre;
  this.curadas = data.personasCuradas;
  this.especialidad = data.especialidad;
}

MedicoInternas.prototype.curarPersona = function () {
  this.curadas++;
  console.info('Persona curada');
};

MedicoInternas.prototype.mostrarDatos = function () {
  console.info('Nombre:', this.nombre);
  console.info('Personas curadas:', this.curadas);
  console.info('Especialidad:', this.especialidad);
}

let medico = new MedicoInternas({
  nombre: 'Leslie',
  personasCuradas: 0,
  especialidad: 'Cardiología'
});

arrancar('curar');
arrancar('curar');
arrancar('curar');
console.log('\n');
arrancar('datos');

function arrancar (action) {
  if (action === 'curar') {
    medico.curarPersona();
  } else {
    medico.mostrarDatos();
  }
}
    
answered by 19.03.2017 / 16:09
source