Vayl
Vayl

Reputation: 91

Logic inside a basic class method not behaving as expected

Fooling around with Classes trying to understand them better and I've found myself stuck with what should otherwise be extremely basic.

The code below is basically just a class extending another and me trying them out. My issue has to do specifically with the logic within the Animal method danger(dangerLvl).

The way it's written just below works as expected:

class Animal {
    constructor(name, age, dangerLvl) {
        this.name = name;
        this.age = age;
        this.dangerLvl = dangerLvl;
    }
    danger(dangerLvl) {
        if (dangerLvl != 0) {
            return `${this.name} is dangerous!`
        } else {
            return `${this.name} is docile :)`
        }
    }
}

class Feline extends Animal {
    hiss() {
        return `${this.name} hisses threateningly!`
    }
}

const sissi = new Feline("Sissi", 5, 1);

sissi.danger();
// returns "Sissi is dangerous!"

What I don't get is the behavior of the method danger(dangerLvl) when written like this:

    danger(dangerLvl) {
        if (dangerLvl >= 1) {
            return `${this.name} is dangerous!`
        } else {
            return `${this.name} is docile :)`
        }
    }

const sissi = new Feline("Sissi", 5, 1);

sissi.danger();
// returns "Sissi is docile :)" <===== which is NOT expected. This is what I don't get.

Not exactly sure how I've managed to get to a point where I somewhat get async functions, callbacks and prototypes but can't figure out something as basic as this. Quite frustrating to say the least.

In my mind I should even be able to write it this way but clearly I'm missing something...

    danger(dangerLvl) {
        if (dangerLvl >= 1) {
            return `${this.name} is dangerous!`
        }
        return `${this.name} is docile :)`
    }

Thanks.

Upvotes: 0

Views: 79

Answers (2)

Alumno Cabreado
Alumno Cabreado

Reputation: 301

I don't understand at all why you use the dangerLvl as argument of Animal.prototype.danger function if you have it on that context, if you don't define the argument it will be passed as undefined and the condition is getting else because undefined >= 1 is false.

class Animal {
    constructor(name, age, dangerLvl) {
        this.name = name;
        this.age = age;
        this.dangerLvl = dangerLvl;
    }
    danger(){
        
        console.log(this.dangerLvl);
    
        if (this.dangerLvl >= 1) {
            return `${this.name} is dangerous!`
        } else {
            return `${this.name} is docile :)`
        }
    }
}

class Feline extends Animal {
    
    hiss() {
        return `${this.name} hisses threateningly!`
    }
}

const sissi = new Feline("Sissi", 5, 1);

console.log(sissi.danger()); //Sissi is dangerous


const sissi2 = new Feline("Rabbit", 5, undefined);

console.log(sissi2.danger()); //Rabbit is docile :)

Upvotes: 0

Amir Saleem
Amir Saleem

Reputation: 3140

Since you are not passing dangerLvl as an argument, it is undefined. If you want to access the dangerLvl class member you need to use this. Here is an example

class Animal {
    constructor(name, age, dangerLvl) {
        this.name = name;
        this.age = age;
        this.dangerLvl = dangerLvl;
    }
    danger() {
        if (this.dangerLvl != 0) {
            return `${this.name} is dangerous!`
        } else {
            return `${this.name} is docile :)`
        }
    }
}

Upvotes: 1

Related Questions