leaderboard

by Vladymyr Shevchuk

JavaScript

class GothamHero {
            #name = 'Bruce Wayne';
            name = 'Batman';

            constructor () {
                this.getName = () => this.name;
            }

            static get static_getter() {
                return "A static getter.";
            }

            static mood() {
                return 'angry';
            }

            get realName () {
              return this.#name
            }
        }

        const hero = new GothamHero();

        console.assert(JSON.stringify(Object.keys(hero)) === JSON.stringify(["name", "getName"]), 'class GothamHero should contain only "name" and "getName" own property');
        console.error(Object.getOwnPropertyNames(hero));
        console.error('hero name', hero.getName());
        console.error('hero real name', hero.realName);
        console.error('hero mood', GothamHero.mood());
        console.error('hero mood', GothamHero.static_getter);

        class Test {
            #private_field = "A private field.";
            public_field = "A public field.";
            static get static_getter() {
                return "A static getter.";
            }
            static static_method() {
                return "A static method.";
            }
            get getter() {
                return "A non-static getter.";
            }
            method() {
                return "A non-static method.";
            }
        }

        console.log(`Class ("${typeof Test}" type)`, Object.getOwnPropertyDescriptors(Test));
        console.log("Its prototype", Object.getOwnPropertyDescriptors(Test.prototype));
        console.log("Its instance", Object.getOwnPropertyDescriptors(new Test));