That question was:
To retrieve the "age" property from an object stored in a variable called "person1," you would use: A. person1[age] B. person1{age} C. age.person1 D. person1.age
You are correct. If I want the "age" property using the bracket syntax, I have to use person1["age"]
Sure. One main difference: map
collects and
returns an array of the return values values; it's all about
the return values of the callback
function. forEach
ignores the return values and
returns nothing.
I put some in the lecture notes.
Yes, it's like a loop. The callback function is like the body of the loop: it's the stuff that is done for each element.
Sure; let's look at the array methods section of the reading.
Yes! (We don't be getting into OOP in this course, but that's how it's done!)
Frequency really isn't the issue. Both languages have objects with methods and these are used in normal programming; they're not weird or exotic. (Take a look at the Python DateTime Objects; the word "method" occurs 36 times on that page.
The important thing is that you feel comfortable using methods. That's why we practice with dates and arrays.
The terminology is unfortunate. Yes, in JavaScript, as in Java, pretty much everything is an object. But we also have objects that act like dictionaries. So, while lists (arrays) are objects (and have methods and such), we need a term for the dictionary objects. The standard is just to call them "objects" and hope the reader can figure it out from context.
I'm going to call them dictionaries, even though that's not standard.
When we get to MongoDB, they might be called documents.
Yes, we can make our own objects, but this course is not going to cover OOP in JavaScript unless you want me to. We can do it at the end of the course.
I'll add some more practice problems.
Differences between Python dictionaries and JavaScript object literals (AKA dictionaries) are very few. Python dictionaries have a few methods that JavaScript dictionaries don't, but the basic square bracket syntax to read and store is identical:
dic['a'] = 3; print(dic['a'])
There is the dot notation which is nice and only available in JavaScript
dic.a = 3; print(dic.a)
For sure. The dot notation is used when we have a constant as the key: in other words, the programmer knows what key they want. This covers the vast majority of cases.
If, on the other hand, the key is unknown, maybe chosen by the user or computed from something else, we must use the square bracket notation, and put the variable in the square brackets.
Again, rare, but it does happen. We'll see some examples.
I'm glad!