Quiz

  1. How does OOP affect how webpages look?

    It doesn't. There's no connection.

    OOP is how we happen to implement the code that dynamically creates the HTML/CSS on the page, but we could have done it in other ways.

    To the extent that HTML and CSS are code, we are writing JS code that writes HTML/CSS code.

    code that writes code is tricky!

  2. var order1 = {emailAddress: key1,
                  coffee: "black with milk and an embarrassing amount of sugar",
                  size: "short",
                  flavor: "mocha",
                  strength: 38
                  }; 
    
    why size, flavor, strength are not in a string format? Thank you!

    Unlike Python, JS requires keys to be strings, and it will automatically stringify the keys, so you don't have to type the string quotes. Consider:

    
        p1 = {"x": 3, y: 4}
        console.log(p.x, p.y);
    
    

    The extra string quotes around x are unnecessary in JS, unlike Python.

  3. So when we're dealing with code where it asks for user input, we're always going to use square bracket notation because the key values depend on the user input?

    Probably, but not always.

    Sometimes, the user will submit data in a known format and we will look for particular things (keys), such as emailAddress, in which case the code might be obj.emailAddress.

    Sometimes, the code is generic and doesn't know what the set of keys are, so it may iterate over them. Like this:

    
        let keys = Object.keys(obj);
        keys.forEach(k => console.log(k, obj[k]);
    
    

    Or it might have the key be an argument as with our KeyValueDB class or Coffeerun's DataStore class

    In the generic situation, when the key is contained in a variable, we have to use the square bracket notation.

  4. Can you elaborate more on APIs? Does it have anything to do with abstraction barrier, private and public classes?

    Yes. An API is a general notion of code interacting with other code.

    Take something simple like a built-in object, say dictionaries, lists or strings.

    They have an API: a set of methods that they support, etc., allowing us to use them.

    We don't know how dictionaries, lists and strings are implemented, and they might be implemented differently in different browsers. But as long as they correctly implement the API, it all works. That's the abstraction barrier

    A class is a way of defining a new kind of object and part of that definition is its API.

  5. Do we have to import APIs like we do in Java? I'm a little confused on how to use them.

    In general, yes, we have to import code in order to use it. (Unless it's built-in, see above.)

    The API is the set of rules of how we use it once it's imported: the functions and methods that we just imported.

  6. APIs are great and allow software components to communicate, does an update on an api affect all software using it? If an API is manipulated by a dangerous party does it affect the softwares that use the said API?

    Yes. If you're able to modify the code of a module that other people import, you can do nefarious things.

    Some things are imported dynamically (e.g. jQuery from a CDN). An attack there gets propagated very quickly.

    Some things are imported sporatically (e.g. jQuery downloaded and stored locally)

  7. Explain more on constructors and instance variables.

    The constructor is a function that is invoked when we say new Classname(constructor_args...).

    It can give initial values to the instance variables.

    Instance variables are how we store data in an object, like the coordinates of the point p1 above.

  8. Can I say the class we are talking about here is like a collection of functions/variables that works as a part?

    Yes, that will work.

  9. Can you clarify truck class and how it differs?

    Differs from what? It is different from all the other classes.

    Its purpose is to store the set of pending orders for a particular "food truck". In principle, we could have several.

    It has methods like createOrder, deliverOrder and printOrders

  10. So when we're dealing with code where it asks for user input, we're always going to use square bracket notation because the key values depend on the user input?