ES6 undefined $ {this.name}

2

Friends I'm trying the following

class Player{

    //todo lo que resiba este const será la clase persona
    constructor(nombre,edad){
        nombre == this.nombre;
        edad == this.edad;

    }

    mA(){
        console.log('la edad es de ${this.edad}')
    }
}


let px = new Player('kyo',21);
px.mA();

but it gives me

  

the age is undefined

Does anyone know why? Am I jumping something?

    
asked by E.Rawrdríguez.Ophanim 30.12.2018 в 03:56
source

1 answer

2

You have the following errors.

  • In the constructor properties are initialized so the operator is = that is equal to and you are wrong to use == which is to compare if the value on the left is equal to the value on the right
  • The constructor receives the properties and initializes them so this.prop = prop
  • Then the final code is:

        class Player{
        
            //todo lo que resiba este const será la clase persona
            constructor(nombre,edad){
                this.nombre = nombre
                this.edad = edad
        
            }
        
            mA(){
                console.log('la edad es de ${this.edad}')
            }
        }
        
        
        let px = new Player('kyo',21);
        px.mA();

    The final result you will get is:

    "la edad es de 21"
    

    IMPORTANT NOTE

  • Using = is to assign a value, that is% a = 9;
  • Using == is to indicate if the left side is equal in value to the right side, for example 10 == 10
  • Using === is to indicate whether the left side is equal in type and value to the right side; for example 10 === 10 that will give true and 10 === "10" will false because the value on the right is a string of text
  • answered by 30.12.2018 / 04:03
    source