Quiz
- The terms regarding dictionary such as Key, property, variable, and constant are a little bit confusing. Would it be possible to review theses terms together?
Sure, let's review:
- variable: a named container for a value, such as
numStudents
orname
- constant: a value, such as
28
orCS 204
- key: uniquely identifies one key/value pair in a dictionary. It's how we get the value out. Eg. Your B-number is how the registrar looks you up in Banner to see your course list.
- property: same as a key. "key" is the terminology used with Python dictionaries, but JS objects are usually described as property/value pairs.
- variable: a named container for a value, such as
- what is the difference between a method and a function?
A method is a function that belongs to a particular class of object. For example,
.toUpperCase()
is a method on strings, and.getFullYear()
is a method on dates, and so forth. Later, we'll learn how to define these. - Can you adjust the visibility of variables and methods (private, public, protected, etc.) in JavaScript?
No; JavaScript is less sophisticated than Java about visibility. It *is* possible to hide things using closures, but it's not commonly done.
I view this as JavaScript being more trusting of its programmers than Java is.
- Should we use quotation marks for keys in objects? On the reading some examples used quotes and some didn't so I just wondered which one was correct. Thanks!
Use quotation marks for literal values, and omit them for variables:
So, just like in English, you use quotation marks when you mean literally this value. Here's a comedy bit that was old before I was born. The comedy duo of George Burns and Gracie Allen would end their routine with:
George Burns: Say goodnight, Gracie.
Gracie Allen: Goodnight Gracie! - I am a little bit confused about this piece of code:
console.log(p1['name']);
in the previous example, the property names don't have the single quotation marks. Do we have to have the single quotation marks so that the p1[] notation would work?This is a topic that a lot of students struggle with. Here's the relevant section: dot notation
The key is *always* a string. Sometimes it's a literal string constant in the square brackets, and sometimes it's stored in a variable. Variables are nice if the key is going to change. Like our
coord
variable, above.If the key is never going to change, the dot notation is a nice shortcut.
- Can we go over unknown properties and it's applications? Thank you!
The example in the reading about unknown properties is iterating over all the keys to, say, print them out. That's a common and important operation.
Another is when we aren't sure which property we are going to want. An example: we have a rectangle with a
width
and aheight
. To determine if the rectangle will fit, we have to compare the larger dimension: - is there a function to get the Object's key values as in a way to get an array in the shape of ['name','gradYear','going']?
Yes. it's the
Object.keys()
function from the unknown properties section. - What is the [[Prototype]] at the end of the console.dir?
We'll talk more about prototypes when we get to OOP implementation. It has to do with inheritance.
- No questions right now, but I'm really enjoying the class!
Great!