The Four States of Javascript References
Posted on 27/03 23:30
A reference in javascript can have one of four distinct states: undefined, null, scalar, or object. If you are familiar with Java you'll recognize null, and object. Scalar is essentially the same thing as a primitive in Java, so that only leaves undefined to be explained. Many languages like Java or C# will check references at compile time. This means that if the compiler encounters some reference that hasn't been defined elsewhere, it will throw an error. This forces the programmer to define all references before compilation will succeed. In Javascript and other interpreted languages there is no compile time checking of references. This occurs during runtime, which means that if you use an reference that wasn't defined somewhere you'll usually encounter a nasty error. This forces the programmer to check whether a reference has been defined in situations where it isn't guaranteed. This might happen if you conditionally include a library file and refer to objects in the library from your code. Just like we learn that null and empty string aren't the same thing when it comes to relational databases, it's important to learn that null and undefined are not the same thing in Javascript.
For instance, this Javascript snippet will fail with the error "abc is not defined":
Once we add a line defining abc, then we can refer to it:
This code will run without error and the alert will not occur.
Assuming abc *might* be defined elsewhere, the way we protect our code against abc not being defined looks like this:
So if abc was never defined, this code prints nothing. If it's been defined but is null, you'll get one alert: "defined!" If it's been defined and isn't null, you'll see both alerts.
More compactly you can write:
The typeof function is useful in Javascript when you aren't sure of the state of a reference. In addition to checking whether it's defined, you can use it to check whether a reference points to an object:
This is useful if you're not sure whether abc is an object or a scalar. If you called abc.method() without this check and abc turned out to be a scalar you'll get this error: "abc is null or not an object."
For instance, this Javascript snippet will fail with the error "abc is not defined":
if (abc != null) alert ("not null!");Once we add a line defining abc, then we can refer to it:
var abc = null;
if (abc != null) alert ("not null!");
This code will run without error and the alert will not occur.
Assuming abc *might* be defined elsewhere, the way we protect our code against abc not being defined looks like this:
if (typeof(abc) != "undefined") {
alert("defined!");
if (abc != null) alert("not null!");
}So if abc was never defined, this code prints nothing. If it's been defined but is null, you'll get one alert: "defined!" If it's been defined and isn't null, you'll see both alerts.
More compactly you can write:
if (typeof(abc) != "undefined" && abc != null) do something wth abc...
The typeof function is useful in Javascript when you aren't sure of the state of a reference. In addition to checking whether it's defined, you can use it to check whether a reference points to an object:
if (typeof(abc) == "object") abc.method();
This is useful if you're not sure whether abc is an object or a scalar. If you called abc.method() without this check and abc turned out to be a scalar you'll get this error: "abc is null or not an object."
My LinkedIn profile
Posted on 09/02 15:03
My professional profile is available on LinkedIn.com:
http://www.linkedin.com/in/dudcat
http://www.linkedin.com/in/dudcat
Will blog for $$$
Posted on 21/09 15:00
I just read an interesting article from Business 2.0 about people out there blogging for a living. Now I just have to figure out what to blog about, and then get a dedicated following of millions, and I'll be rolling in the dough!
;)
;)
Dumb and Dumberer
Posted on 31/08 16:26
Just read on CNN that nationwide SAT scores this year had their biggest drop in 31 years. Various reasons are given, including the fact that the test format changed this year and the price to take the test went up.
Anecdotally it seems like SAT scores have been on this downward trend for decades so I did some searching and found an interesting paper about the SAT test. It made me realize that designing an ideal test is a hard if not impossible task, and that as much as the College Board says otherwise all those SAT coaching programs really can make an impact. Which goes back to the argument whether the test reflects aptitude or demographics - because there definitely is a correlation because family income and the likelihood a student will enroll in a coaching program. I also liked the part where the author mentions that the students of 1926 (the first year the SAT was administered) faired poorly because they felt guessing on a multiple choice test was immoral!
Anecdotally it seems like SAT scores have been on this downward trend for decades so I did some searching and found an interesting paper about the SAT test. It made me realize that designing an ideal test is a hard if not impossible task, and that as much as the College Board says otherwise all those SAT coaching programs really can make an impact. Which goes back to the argument whether the test reflects aptitude or demographics - because there definitely is a correlation because family income and the likelihood a student will enroll in a coaching program. I also liked the part where the author mentions that the students of 1926 (the first year the SAT was administered) faired poorly because they felt guessing on a multiple choice test was immoral!
Posting from my phone
Posted on 05/03 12:36
So I just got a Treo 650 and here's my first post from it. If you are into gadgets, you definitely have to get one of these! Google maps on the go is awesome!
America's collective shopping binge still going strong
Posted on 01/03 15:18
According to this article, Americans have been spending more than they've made since March of last year. Where did all this borrowed money come from? A lot has come from our newly minted credit cards, also known as our homes.
Equity loans are so easy to get, and readily accessible thanks to the record breaking real estate appreciation we have experienced in the past few years. These loans make sense when used to pay off higher interest rate credit cards, or to cover some medical emergency. But using it to effectively give yourself a boost in salary so you can buy the latest gadget or toy is a risky move that can end up hurting you when rates go up.
Equity loans are so easy to get, and readily accessible thanks to the record breaking real estate appreciation we have experienced in the past few years. These loans make sense when used to pay off higher interest rate credit cards, or to cover some medical emergency. But using it to effectively give yourself a boost in salary so you can buy the latest gadget or toy is a risky move that can end up hurting you when rates go up.
Author: Brandon
Category: Real Estate
Austin, Texas Real Estate Resources
Posted on 24/02 10:24
Here are some sites I found really useful during my search for property in Austin:
http://www.texasdavid.com
- Tons of free resources on this site, MLS listings, Cash flow calculator
http://recenter.tamu.edu
- Lots of information on Texas real estate, quarterly "Tierra Grande" magazine is very useful
http://www.statesman.com/business/content/business/realestate/index.html
- Listings, trends, and the Neighborhood Profile section is very helpful for investors who are not from Austin
http://www.texasdavid.com
- Tons of free resources on this site, MLS listings, Cash flow calculator
http://recenter.tamu.edu
- Lots of information on Texas real estate, quarterly "Tierra Grande" magazine is very useful
http://www.statesman.com/business/content/business/realestate/index.html
- Listings, trends, and the Neighborhood Profile section is very helpful for investors who are not from Austin
Author: Brandon
Category: Real Estate
Kristal and me as South Park characters
Posted on 10/02 13:35

Kristal aka Nameless Girlfriend

Brandon "I like Beer" Duncan
The great turkey ride of 2005
Posted on 28/11 23:48
So Kristal and I decided to have our Thanksgiving dinner at her place in Santa Cruz. I had wanted to try riding my bike from my place to Santa Cruz, so I figured the hot turkey (in our case, Trader Joe's fish) dinner waiting for me would be incentive to pedal hard.
So I set out from my place around noon and rode across San Jose to Los Gatos and up to the Lexington dam. I was already tired by the time I got to the top, and my journey was just begining. I still had to pedal over the Santa Cruz mountains! Here's a view looking out over the resevoir:

From here I rode around the west side of the resevoir on a single track to Old Santa Cruz highway which parallels Highway 17 for a while. This is where things got really tough. I won't lie, I had to stop quite a few times, but I was pedalling up to a 2000 foot elevation.
Not much up there except trees and lots of curves in the roads, but I did pass an inflatable turkey which seemed to mock me as I pedalled by spinning the cranks as fast as I could, yet only managing a snail's pace:

By the time I made it to the summit 3 hours had already passed and my legs were numb from the pain, and not even a strategically packed Power Bar could bring them back to life. Luckily, the ride down was a breeze. Literally! I coasted for several minutes at a time, getting up to speeds of 40mph. Fast enough to give a "wazzup" to the motorcyclist who came alongside me. I don't want to think what would have happened to me if I hit a rock in the road at that speed!
Around 4pm I limped into Kristal's apartment and proceeded to scarf down everything in site:

Of course now that I've checked that off of my "things to do before I die" list, I was in no hurry to do it again, so I packed my bike into Kristal's car and got a lift back to my place, with a newfound appreciation for powered transportation!
So I set out from my place around noon and rode across San Jose to Los Gatos and up to the Lexington dam. I was already tired by the time I got to the top, and my journey was just begining. I still had to pedal over the Santa Cruz mountains! Here's a view looking out over the resevoir:

From here I rode around the west side of the resevoir on a single track to Old Santa Cruz highway which parallels Highway 17 for a while. This is where things got really tough. I won't lie, I had to stop quite a few times, but I was pedalling up to a 2000 foot elevation.
Not much up there except trees and lots of curves in the roads, but I did pass an inflatable turkey which seemed to mock me as I pedalled by spinning the cranks as fast as I could, yet only managing a snail's pace:

By the time I made it to the summit 3 hours had already passed and my legs were numb from the pain, and not even a strategically packed Power Bar could bring them back to life. Luckily, the ride down was a breeze. Literally! I coasted for several minutes at a time, getting up to speeds of 40mph. Fast enough to give a "wazzup" to the motorcyclist who came alongside me. I don't want to think what would have happened to me if I hit a rock in the road at that speed!
Around 4pm I limped into Kristal's apartment and proceeded to scarf down everything in site:

Of course now that I've checked that off of my "things to do before I die" list, I was in no hurry to do it again, so I packed my bike into Kristal's car and got a lift back to my place, with a newfound appreciation for powered transportation!
Quick dinner recipe
Posted on 23/11 02:51
Kristal and I put together a delicious meal the other night from some ingredients we had around. It was a bit of an adventure since we weren't going off any recipe, so we basically mixed a bunch of stuff together and crossed our fingers.
But it turned out great. I call our creation Dijon pork with Couscous salad.
Here's how we made it:
1. Get some Trader Joe's whole wheat couscous and prepare about 1/4 of the box. Put the couscous in the fridge to cool.
2. Defrost two pork loin chops. I got some from Omaha Steaks. These guys are great. You can order over the internet and they ship the stuff on dry ice from Nebraska. Preheat the oven to 400 degrees.
3. Pierce the pork with a fork a few times, and rub about 5 tablespoons worth of Grey Poupon (or other Dijon mustard) on it. Sprinkle some dried Thyme, salt, and pepper on both sides of the pork.
4. Heat a couple tablespoons of olive oil on a skillet over high heat. Brown each side of the pork (about 2 1/2 minutes on each side). Now, assuming the skillet is ovenproof, throw the whole thing in the oven on the middle rack for about 15 minutes.
5. While the pork is in the oven, take the cold couscous and mix the following ingredients: sliced Kalamata olives, freshly chopped parsley (yes, it's more than just a garnish!), chopped tomatoes, and a generous amount of olive oil.
6. Serve and enjoy!
Here's a picture of the dishes - note the pork hasn't been cooked yet. I made everything in a different order than I describe here, but I realized after the fact there was a more efficient way to do it.
But it turned out great. I call our creation Dijon pork with Couscous salad.
Here's how we made it:
1. Get some Trader Joe's whole wheat couscous and prepare about 1/4 of the box. Put the couscous in the fridge to cool.
2. Defrost two pork loin chops. I got some from Omaha Steaks. These guys are great. You can order over the internet and they ship the stuff on dry ice from Nebraska. Preheat the oven to 400 degrees.
3. Pierce the pork with a fork a few times, and rub about 5 tablespoons worth of Grey Poupon (or other Dijon mustard) on it. Sprinkle some dried Thyme, salt, and pepper on both sides of the pork.
4. Heat a couple tablespoons of olive oil on a skillet over high heat. Brown each side of the pork (about 2 1/2 minutes on each side). Now, assuming the skillet is ovenproof, throw the whole thing in the oven on the middle rack for about 15 minutes.
5. While the pork is in the oven, take the cold couscous and mix the following ingredients: sliced Kalamata olives, freshly chopped parsley (yes, it's more than just a garnish!), chopped tomatoes, and a generous amount of olive oil.
6. Serve and enjoy!
Here's a picture of the dishes - note the pork hasn't been cooked yet. I made everything in a different order than I describe here, but I realized after the fact there was a more efficient way to do it.
