Short JS task for Monday

    image

    Which was born in the process of studying the LexicalEnvironment nightmares and generally educational program on the topic "Why does it work like that."


    The task really came from the wtfjs series, but after careful consideration, it completely fits into the logic of the language.


    So, right to the point:


    There is a module module.js:


    // module.js
    function getRandomNumber() {
        return Math.floor(Math.random() * 100)
    }
    var a = 10;
    function b() { 
        a = 100;
        return;
        function a() {}
    }
    b();
    module.exports = getModule();
    async function getModule() {
        return {
            a,
            get getterA() {
                return a
            },
            x: ++a + ++a,
            rndA: a + getRandomNumber(),
            getA: function() { return this.a },
            getArrowA: () => this.a,
            getRndA: () => a + getRandomNumber()
        }
    }

    It is called from index.js file:


    // index.js
    a = 5;
    (async () => {
      let moduleObj1 = await require("./module")
      let moduleObj2 = await require("./module")
      console.log(
        `
        ============================
        m1Obj.a:              | ${moduleObj1.a}
        m1Obj.getterA:        | ${moduleObj1.getterA}
        m1Obj.x:              | ${moduleObj1.x}
        m1Obj.getA:           | ${moduleObj1.getA()}
        m1Obj.getArrowA:      | ${moduleObj1.getArrowA()}
        m1Obj.rndA:           | ${moduleObj1.rndA}
        m1Obj.getRndA:        | ${moduleObj1.getRndA()}
        ============================
        m2Obj.a:              | ${moduleObj2.a}
        m2Obj.getterA:        | ${moduleObj2.getterA}
        m2Obj.x:              | ${moduleObj2.x}
        m2Obj.getA:           | ${moduleObj2.getA()}
        m2Obj.getArrowA:      | ${moduleObj2.getArrowA()}
        m2Obj.rndA:           | ${moduleObj2.rndA}
        m2Obj.getRndA:        | ${moduleObj2.getRndA()}
        ============================
        a:                    | ${a}
        ============================`
      )
    })()

    Actually, the question is: what will be displayed in the console?


    Answer:
        ============================
        m1Obj.a:              | 10
        m1Obj.getterA:        | 12
        m1Obj.x:              | 23
        m1Obj.getA:           | 10
        m1Obj.getArrowA:      | 5
        m1Obj.rndA:           | рандомное число от 12 до 111
        m1Obj.getRndA:        | рандомное число от 12 до 111
        ============================
        m2Obj.a:              | 10
        m2Obj.getterA:        | 12
        m2Obj.x:              | 23
        m2Obj.getA:           | 10
        m2Obj.getArrowA:      | 5
        m2Obj.rndA:           | рандомное число от 12 до 111
        m2Obj.getRndA:        | рандомное число от 12 до 111
        ============================
        a:                    | 5
        ============================

    Only registered users can participate in the survey. Please come in.

    Task

    • 5.3% I decided the answer was 5
    • 9.6% Decided, the answer did not agree completely 9
    • 0% Decided, the answer did not agree 0
    • 84.9% Spit and did not decide, the results are interesting 79

    Also popular now: