Van icon

JAVASCRIPT CLASSES

Links


    // 90 - Use class syntax to Define a constructor function

    // var SpaceShuttle = function(targetPlanet){
    //     this.targetPlanet = targetPlanet;
    // }

    class SpaceShuttle {
        constructor(targetPlanet){
            this.targetPlanet = targetPlanet; 
        }
    }
    console.log("Constructor");
    var zeus = new SpaceShuttle('Jupiter');
    console.log(zeus.targetPlanet);
    


    // 91 - GETTERS AND SETTERS

    class Book {
        constructor(author){
            this._author = author;
        }

        get writer(){
            return this._author;
        }

        set writer(updatedAuthor){
            this._author = updatedAuthor;
        }
    }

    function makeClass(){
        class Thermostat{
            constructor(temp){
                this._temp = 5/9 * (temp - 32);
            }

            get temperature(){
                return this._temp;
            }

            set temperature(updatedTemp){
                this._temp = updatedTemp;
            }
        }
        return Thermostat;
    }

    const Thermostat = makeClass();
    const thermos = new Thermostat(76);
    let temp = thermos.temperature;
    thermos.temperature = 26;
    temp = thermos.temperature;
    console.log("Getters and Setters");
    console.log(temp);