GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing justjavascript.com — Properties Meet Sherlock Holmes, a world-renowned detective from London:
His friend, John Watson, recently moved in:
Sherlock is a brilliant detective, but a difficult flatmate. One day, John decides he’s had enough—he changes his surname and moves to Malibu:
Time for a small exercise. Write down your answers to these questions:
Before re-reading the code, I want you to approach these questions in a particular way. Use a sketchpad or get paper and a pen, and sketch out your mental model of what happens on every line. It’s okay if you’re not sure how to represent it. We haven’t yet discussed these topics, so use your best guess. Then, with the help of your final sketch, answer the four questions above. Answer Don’t reveal until you have written the answers to four questions. Answer Now let’s check your answers:
This is not a typo—they are indeed both in Malibu. It’s not so easy to get away from Sherlock! With an inaccurate mental model, one might conclude that To see why, we need to learn how properties work in our JavaScript universe. PropertiesWe’ve talked about objects before. For example, here is a
In our universe, it might look like this:
However, objects are primarily useful to group related data together. For example, we might want to group different facts about Sherlock:
Here, In our JavaScript universe, both variables and properties act like “wires.” However, the wires of properties start from objects rather than from our code:
Here, we can see that the Importantly, properties don’t contain values—they point to them! Remember that our universe is full of wires. Some of them start from our code (variables), and others start from objects (properties). All wires always point to values. Before reading this, you might have imagined that values live “inside” objects because they appear “inside” in code. This intuition often leads to mistakes, so we will be “thinking in wires” instead. Take one more look at the code and the diagram. Make sure you’re comfortable with them before you continue. Reading a PropertyWe can read a property’s current value by using the “dot notation”:
Here, It leads to an object. From that object, JavaScript follows the Our object’s Property NamesOne important thing to keep in mind when naming a property is that a single object can’t have two properties with the same name. For example, our object can’t have two properties called Property names are also always case-sensitive! For example, If we don’t know a property name ahead of time, but we have it in code as a string value, we can use the
Try this code in your browser console and enter Assigning to a PropertyWhat happens when we assign a value to a property?
Let’s split this code into the left and the right side, separated by First, we figure out which wire is on the left side: We follow the Note that we don’t follow the Remember which wire we picked? Carry on. Next, we figure out which value is on the right side: Unlike the left side, the right side of an assignment always expresses a value. In this example, the right side’s value is the number value Now we are ready to perform the assignment. At last, we point the wire on the left side to the value on the right side: And we’re done! From now on, reading Missing PropertiesYou might wonder what happens if we read a property that doesn’t exist:
We know that JavaScript uses a set of rules that looks something like this:
These rules are a bit simplified, but they already tell us a lot about how JavaScript works! For example,
Note this does not mean that our object has a
It is tempting to think It looks at the object that Fun Fact Fundamentally, it’s because every expression needs to result in some value, or throw an error. Some other languages throw an error if you access a property that doesn’t exist—but JavaScript is not one of them! Scroll up and re-read the rules again. Can you apply them in practice?
What happens if we run this code? Don’t guess—follow the rules. Hint: there are two dots, so you need to follow the rules two times. Answer Don’t reveal until you have finished writing. Answer The answer is that calculating
If this still seems confusing, scroll up and mechanically follow the rules. Recap
Note that this mental model of properties is still a bit simplified. It is good enough for now, but it will need to expand as you learn more about the JavaScript universe. If you got confused by the Sherlock Holmes example in the beginning, try walking through it again using our new mental model and focusing on properties as wires. The next module will include a detailed walkthrough in case you’re still not quite sure why it works this way. ExercisesThis module also has exercises for you to practice! Don’t skip them! Even though you’re likely familiar with the concept of properties and assignment, these exercises will help you cement the mental model we’re building. We need this foundation before we can get to more complex topics. |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |