Quiz

  1. I'm slightly confused with question 2 of the quiz. Are constructors functions that create objects or initializes the instance variables of a new object? From what I understand these are different things but might be wrong.

    They are slightly different, but the distinction doesn't really matter because they are always used together.

    They are called "constructors" but it's really the new operator that creates the object.

  2. Why don't we need "var" before coffee_database? (Referring to in-memory key-value databases.)

    We ought to, but JS is pretty sloppy and I usually don't bother when I'm putting test code in the JS console. I'll add that keyword to the reading, though. Thanks

  3. Can we do coffee_database = {key1: order1}; ?

    Not and have it mean what you want it to mean. Let's try it!

    
    var key1 = 'scott@wellesley.edu';
    var order1 = {emailAddress: key1,
                  coffee: "black with milk and an embarrassing amount of sugar",
                  size: "short",
                  flavor: "mocha",
                  strength: 38
                  };
    var coffee_database = {key1: order1};
    coffee_database[key1] = order1;
    
    

    When we use an object literal, the keys are implicitly quoted.

    This is very important, because it helps clarify the differences among the following:

    
    var db = {};
    db.x = 3;
    db['y'] = 4;
    db[z] = 5;
    
    
  4. What does object.keys(vals) do in the cofee data store class?

    It returns an array of the keys of an object. Try it:

    
    Object.keys(coffee_database);
    Object.keys(db);
    dic = {a: 1, b: 7, c: 3, d: 5};
    Object.keys(dic)
    
    
  5. Could you give an example of how to use/call the value of an object that itself is a value within another object? ie. How to reference VALUE in
    obj = { key1: {key2: VALUE}}

    Like this:

    obj.key1.key2

    Try it in the JS console!

    
        obj = { key1: {key2: 5}};
        obj.key1.key2;
    
    
  6. What's the difference between the .add() method and the .put() method?

    None! Just different names for a similar operation. Part of the craft of an API is making up names: you want to pick names that are succinct, consistent, meaningful and intuitive. But those are all judgments or matters of opinion.

    "Get" vs "put" are common pairs, as are "get" vs "set".

    But someone thought "add" was intuitive for adding a coffee order to a database.

  7. I'm pretty confused with API, can you explain more about it?

    For sure. Take an even simpler case: consider the Math.sqrt function. Do you care how it's implemented? No. You only care about the name of the function, its arguments and its return value. That's the API of Math.sqrt

    If someone comes along with a better algorithm to compute a square root, they can modify the code and, as long as they don't change the API, you don't have to change your code. It just runs better.

  8. What is behind abstraction barrier is a little bit unclear. Please give more explanation!

    The *implementation* of each method is behind the barrier. The *names* of the class and the methods are *visible*. That's the part of the API that you can't change. But you *can* change the implementation.

    We can swap out a new and improved class for an existing class, and the client code doesn't have to change.

  9. This is not directly related, but is there a difference between the API we are learning in this reading vs API add ons that can be used in google sheets to load new data?

    No! Great point! The notion of an API is very, very general and common. It's used whenever one kind of code needs to talk to another kind of code, like when a Google Sheet document needs to talk to a server to load new data.