Question about if inside a class in javascript

2

I wanted to do that if you have a child it is true to show me something, to do it but not inside a class and if it is false tell me that you do not have children, I made this churro but it does not go, this._hijos is true you should enter the if no to the else but it's not going, could someone help me? Thanks

class Persona{
  constructor(nombre, apellido, edad, cm, hijos, celiacos){
    this._nombre = nombre;
    this._apellido = apellido;
    this._edad = edad;
    this._cm = cm;
    this._hijos = hijos;
    this._celiacos = celiacos;
  }

   presentacion(hijos){
    if (hijos) {
      return 'Hola mi nombre es ' + this._nombre + ' y tengo ' + this._edad + 'años, ' + 'mido ' + this._cm + ' cm ' + ' y tengo hijos';
    } else {
      return 'Hola mi nombre es ' + this._nombre + ' y tengo ' + this._edad + 'años, ' + 'mido ' + this._cm + ' cm ' + ' y no tengo hijos';
    }

  }

  gritar(volumen = ' a un volumen desconocido'){
    console.log(this._nombre +  ' ha pegado un grito' + volumen)
  }
}

let amigo1 = new Persona('ivan', 'garcia lopez', 33, 185, true, true)
let amigo2 = new Persona('Fran', 'Manrique', 33, 185, false, true)

console.log(amigo1.presentacion());
amigo1.gritar(' a un volumen estratosferico')
amigo2.gritar();
    
asked by francisco dwq 14.01.2018 в 13:12
source

2 answers

2

If you have it in the constructor you do not need to pass the parameter, remove it from the function presentacion and then the conditional should be:

if (this._hijos) {
    
answered by 14.01.2018 / 16:57
source
2

You are assigning an argument that you will receive presentacion() but you do not give any. Just pass the argument and you're done

class Persona{
  constructor(nombre, apellido, edad, cm, hijos, celiacos){
    this._nombre = nombre;
    this._apellido = apellido;
    this._edad = edad;
    this._cm = cm;
    this._hijos = hijos;
    this._celiacos = celiacos;
  }

   presentacion(hijos){
    if (hijos) {
      return 'Hola mi nombre es ' + this._nombre + ' y tengo ' + this._edad + 'años, ' + 'mido ' + this._cm + ' cm ' + ' y tengo hijos';
    } else {
      return 'Hola mi nombre es ' + this._nombre + ' y tengo ' + this._edad + 'años, ' + 'mido ' + this._cm + ' cm ' + ' y no tengo hijos';
    }

  }

  gritar(volumen = ' a un volumen desconocido'){
    console.log(this._nombre +  ' ha pegado un grito' + volumen)
  }
}

let amigo1 = new Persona('ivan', 'garcia lopez', 33, 185, true, true)
let amigo2 = new Persona('Fran', 'Manrique', 33, 185, false, true)

console.log(amigo1.presentacion(amigo1._hijos)); // Argumento
amigo1.gritar(' a un volumen estratosferico')
amigo2.gritar();
    
answered by 14.01.2018 в 15:28