JSFiddle - React, Tailwind, and code Playground

by Andrew Gerst

JavaScript

// Getters and Setters

var id = 3;

switch (id) {
case 1:
    var lost = {
        loc: "Island",
        get location (){
            return this.loc;
        },
        set location(val){
            this.loc = val;
        }
    };
    
    alert(lost.location);
    lost.location = "Another island";
    alert(lost.location);
break;
case 2:
    function Lost(){
        // Constructor
    }

    Lost.prototype = {
        get location (){
            return this.loc;
        },
        set location(val){
            this.loc = val;
        }
    };
    var lostIsland = new Lost();
    lostIsland.location = "Somewhere in time";
    alert(lostIsland.location);
break;
case 3:
    Object.defineProperty(document.body, "description", {
        get: function (){
            return this.desc;
        },
        set: function (val){
            this.desc = val;
        }
    });
    document.body.description = "Content container";
    alert(document.body.description);
break;
}