Quiz

  1. I would like to talk more about the .keys() function.

    Glad to. It's similar to the Python method of the same name: dic.keys() returns a sequence of the keys in the given dictionary. The equivalent in JS is Object.keys(dic) which is a method on a global variable called Object. It also returns a list of the keys. Here's an example:

    
        let her = {name: "Hermione", hair: "bushy brown", house: "Gryffindor"};
        console.log("Here's what we know about her:", Object.keys(her));
    
    

    In practice, as in Python, we would typically use that information to iterate over the object and do stuff with it, say, print it out:

    
        let her = {name: "Hermione", hair: "bushy brown", house: "Gryffindor"};
        console.log("Here's what we know");
        Object.keys(her).forEach(p => console.log(p, ": ", her[p]))
    
    
  2. Is there a relationship between namespaces and closures? since closures keep variables in their own scope

    Yes! The "inside" or "execution context" of a function creates a new namespace, extending the global or existing namespace.

    
        // global namespace
        var ans = 17;
        function outer(x) {
            // new namespace, with ans, x, a, and b        
            let a = 1;
            const b = 2;
            function inner(y) {
                // yet another new namespace, adding y and c
                let c = 3;
                ... 
            }
        }
    
    

    Closure variables are non-local, non-global names in those inner namespaces.

    Modules are all about global names.

  3. Can you only import something to a file if its origin file explicitly exports it?

    Yes, that's right. The module has to explicitly export the name.

    This is different from Python, where you can get access to everything, even if maybe you shouldn't.

    Reasonable people could disagree on which is better: Is Python too trusting? Is JS too suspicious?

    But, those are the (different) rules of the game.

  4. Can you talk more about globalThis? Why can console access the javascript file we used in plotting assignment but not modules?

    You can read more than you ever wanted to know about globalThis.

    The key idea is that it was introduced to create a standard way of accessing the global environment/namespace.

    It's useful to us because it allows code in a module to set global variables that aren't restricted that module.

    
        // inside module "fred"
    
        // foo is a global variable available in this module, but only in this module
        var foo = 42;   
    
        // bar is truly global, available everywhere, to every module
        globalThis.bar = 17;  
    
    

    Honestly, globalThis is a violation of the separation that modules are intended for. It's a loophole. Use it sparingly and thoughtfully.

    I would really only use it for debugging. For production code, I would stick to defined interfaces.

  5. I am a little bit confused by strict mode.

    It patches what many people think was a mistake in the overly permissive early versions of JS.

    Consider the assignment statement inside the function foo:

    
        var ans = 42;
        function foo() {
            asn = 17;
        }
    
    

    Which of the following is it?

    1. Is that setting a new global variable called asn?
    2. Is it a typo, where the programmer intended to set the existing global variable ans?

    Option 1 is the original JS. Option 2 is strict mode.

  6. Currently no, thanks!

    Great!