0

I'm a bit confused about the "this" keyword in classes in javascript, normally the "this" keyword refers to objects only in javascript, and classes in javascript are just functions with syntax suger, but it seems you can use the "this" keyword in a class to refer to the class it's self, which from my understanding is just a function. You can use the "this" keyword to refer to internal methods. I checked the mdn for the "this" keyword however I didnt see anything on its use with classes. Could someone shed some additional light on this subject and how this works, I'm looking for greater understanding, because this use seems random and inconsistent with how "this" is supposed to be used in javascript. Does the "this" keyword no longer refer to just objects? To be clear I'm not asking how "this" works in general, I'm specifically asking how this works with classes, and how "this" works with a class because a class is a function.

I have static class method on the Dog class, named breed, this uses the "this" keyword to refer to the class and its works fine

    class Dog{
        static breed(dog1,dog2,dogName){
            if(this.isSameBreed(dog1,dog2) && this.isDifferentGender(dog1,dog2)){ //notice the "this" refering to the class   
                               //itself, which is really just a function
                let newDogName=dogName;
                function randomGender(){
                    let randomNum=Math.floor(Math.random() * 2) +1;
                    if(randomNum===1){
                        return "male"
                    }
                    else {
                        return " female"
                    }
                }
                let newDog= new Dog(newDogName,dog1.breed,1,randomGender())
                return newDog;
            }
            else if(dog1.breed != dog2.breed && dog1.breed === dog2.breed){
                console.log(dogs1.name + " is not the same breed, its a dog1.breed and " + dog2.name + " is a dog2.breed");
            }
            else if(dog1.gender === dog2.gender && dog1.breed===dog2.breed){
                console.log("Both dogs are a dog1.gender");
            }
            else{
                console.log(dog1.name + " and " + dog2.name + " are different breeds and same gender");
            }
        }
        static isSameBreed(dog1,dog2){
            return dog1.breed===dog2.breed;
        }
        static isDifferentGender(dog1,dog2){
            return dog1.gender != dog2.gender;
        }
        constructor(name,breed,age,gender){
            this.name=name;
            this.breed=breed;
            this.age=age;
            this.gender=gender;
        }
        bark(){
            console.log(this.name + ": bark bark bark!!!");
        }
        growl(){
            console.log(this.name + ": ggrrroooowwwwl!!!");
        }
    }
Samatha
  • 35
  • 5
  • Methods in a class are syntactic sugar for functions that are in properties of the object prototype. `this` is still an object, and `this.isSameBreed` is a property of the object. – Barmar Mar 17 '17 at 23:54
  • The key is that *this* is set by how a function is called (except for *bind*), it's not lexical (except for arrow functions). – RobG Mar 18 '17 at 00:28
  • Thanks that helps alot! – Samatha Mar 18 '17 at 06:59

0 Answers0