Quiz
- I thought that we had to add () when referencing an object? In the previous question I chose person1.age but i thought it would be person1.age() ?
parens are for invoking a function. While it's possible that person1.age is a function that needs to be invoked, it's more likely to be a number.
In the question, I said it was a property, so we just look up the property using a dot. If the property happened to be a function, we could invoke it, but I didn't ask you to.
- Should we always use dot notation?
Yes. Unless you can't:
- I would like to talk more about Unknown Properties.
It just means that the programmer doesn't know what the key is when they are writing the program. An example is the previous case, where we are looking up a student in the class dictionary.
- I am confused about the syntax of using object.keys for unknown properties and where each part of the syntax comes from.
Good question. The
Object.keys()
function is similar to the Python.keys()
method: it returns a list of the keys of the dictionary.Given the
class_dict
above, we can get a list of students in the class like this: - Do we never delete data in an JavaScript Object? Does the array methods like pop and shift works when removing data in objects as well?
It's rare to delete data, so while it's good to know about, it's less important .
Array methods handle missing elements in different ways:
Loops handle them differently as well:
pop
andshift
usually don't leave any holes in the array, so they are less of a concern. - The reading is straightforward. Since JavaScript is similar to Java I'm able to understand the syntax! / the reading made sense
Glad to hear it!