Quiz

  1. I did not get what question 4 is asking
    var x = {};
    x instanceof Object
    
    A is true
    B is false
    C could be true or false; it depends
    D. cannot be told from this code
        

    Sorry that it was confusing. The question was about the value of the second line of code. The first stores a new, empty dictionary object into the variable x. The second is a boolean expression.

    The value of that expression is true, because x is an instance of an object, hence A.

  2. Since we're talking about info that will or will not be seen by the client, how do we hide code from the user while still letting the website be acessed by them?

    Great question. I have several responses:

    • we don't necessarily have to hide the code from the user. For the vast majority of websites that use JS, the code is visible. (It might be minified, which makes it less readable, but it's not a secret.)
    • However, because you can't modify their code, they are free to change it without affecting your usage. If you could modify their code, you might use/modify something that they remove in the next release.

      For example, you're reading the jQuery source code and you find some getParent internal method that is very efficient because it bypasses some of the checking that the published .parent() does. So, you decided to use that instead. But then in the next release of jQuery, they've removed/renamed that method and now your code breaks. That's your fault for relying on an internal feature of the implementation, rather than sticking to the published, documented interface.

    • If you want to keep your code secret, you could try obfuscating it, as I do with the solution code. But that's not irreversible. It's more like extreme minification, making it harder to read, but not impossible.
    • If you really need to keep the code secret, you need to only distribute the binary code or, for JS, keep it on the server and just provide an HTTP API.
  3. toString() { // using the new template strings return `[A ${this.color} ${this.constructor.name}]`; } Can you explain why do we need to surround this.color with ${}?

    Sure. These are very much like Python's f-strings, which you've probably used, just with a slightly different syntax. See MDN template literals.

    
        # python f-string
        x = 3
        y = 4
        print(f'{x} + {y} is {x+y}')
    
    

    The equivalent in JS:

    
        // JS template literals
        const x = 3
        const y = 4
        console.log(`${x} + ${y} is ${x+y}`)
    
    

    We haven't covered these before, just because of time, but you're welcome to use them.

  4. Could you elaborate more on the instanceof operator in the reading where we checked the inheritance chain for r1?

    Could you explain more about the "instanceof"? I feel a bit confused about how it can check the prototype chain

    I'd be glad to. See MDN instanceof for more.

    In a nutshell, this is a built-in operator (like + or &&) that searches the prototype chain of the left operand for the right operand, evaluating to true iff it's found.