The other day I was talking to a couple of friends about a ReferenceError they were getting in some code they were running. They were just checking for the existence of a global variable. They were just doing this:
if(foo){
doSomething();
}
Would you expect that to work? If so, you’re better at this than I am. If foo is not defined, either in the current scope or globally, you’ll get a ReferenceError there. Why does this strike me as so inconsistent? Consider the following:
var foo = {};
if(foo.propertyThatDoesntExist){
doSomething();
}
If you check for the existence of a property of an object that doesn’t exist, it simply returns undefined. No error is thrown, your code doesn’t halt execution, things don’t explode. In fact, the interpreter will work its way up the scope chain from foo, looking for the missing property.
Now, consider this example:
var setImplicitGlobal = function(){
implicitGlobal = 'Are you sure this is what you wanted?';
}
setImplicitGlobal();
alert(implicitGlobal); // Alerts the string from above
So when we’re doing assignment and try to assign a value to an undefined variable, the interpreter assumes we wanted a new global variable and creates it for us. Despite the fact that this is almost never what anyone would reasonably want to happen.
Finally, consider the following:
if(foo){ // Reference error when foo has not been declared
doSomething();
}
if(window.foo){ // Undefined when foo window.foo has not been set.
doSomething()
}
So when we try to assign a value to an undeclared variable, the interpreter assumes we meant window.foo and creates it for us. But when we’re trying to read the same thing, it doesn’t make the same assumption, throws an error, and breaks our script even when we thought we were coding defensively.
What am I missing here? This is sort of inconsistent, is it not?
Purchase@Abilify.Without.Prescription” rel=”nofollow”>..…
Buygeneric meds…
Posted by SALVADOR on July 29th, 2011.
Order@Cheap.Acai” rel=”nofollow”>……
Buynow it…
Posted by JEFFREY on July 30th, 2011.
Cheap@Generic.Acai” rel=”nofollow”>..…
Buyit now…
Posted by TERRENCE on July 31st, 2011.
Buy@Discount.Coral.Calcium” rel=”nofollow”>..…
Buywithout prescription…
Posted by DONNIE on July 31st, 2011.
Purchase@Cheap.Coral.Calcium” rel=”nofollow”>.…
Buynow it…
Posted by ENRIQUE on July 31st, 2011.
Purchase@Cheap.Coral.Calcium” rel=”nofollow”>……
Buywithout prescription…
Posted by ROY on August 2nd, 2011.
Purchase@Abilify.Without.Prescription” rel=”nofollow”>..…
Buygeneric pills…
Posted by EDUARDO on August 3rd, 2011.
Buy@Discount.Acai” rel=”nofollow”>……
Buygeneric drugs…
Posted by WILLIE on August 3rd, 2011.
Order@Cheap.Acai” rel=”nofollow”>..…
Buynow it…
Posted by CHARLIE on August 3rd, 2011.
Get@Acai.Online” rel=”nofollow”>……
Buygeneric drugs…
Posted by WALLACE on August 4th, 2011.
Buy@Acai.Without.Prescription” rel=”nofollow”>.…
Buygeneric drugs…
Posted by VICTOR on August 4th, 2011.
Buy@Discount.Energy.Boost” rel=”nofollow”>.…
Buynow it…
Posted by TONY on August 5th, 2011.
Cheap@Accupril.Online” rel=”nofollow”>.…
Buygeneric drugs…
Posted by BILLY on August 6th, 2011.
Buy@Accutane.Online” rel=”nofollow”>.…
Buygeneric drugs…
Posted by JIMMY on August 6th, 2011.
Buy@Cheap.Accutane” rel=”nofollow”>.…
Buygeneric drugs…
Posted by SAM on August 6th, 2011.
Cheap@Accutane.Online” rel=”nofollow”>.…
Buygeneric meds…
Posted by CARL on August 6th, 2011.
Order@Aciphex.Online” rel=”nofollow”>.…
Buyno prescription…
Posted by ADAM on August 7th, 2011.
Buy@Generic.Actonel.35mg” rel=”nofollow”>.…
Buyno prescription luw…
Posted by SERGIO on August 10th, 2011.
Generic@Actonel.35mg.Without.Prescription” rel=”nofollow”>..…
Buygeneric pills zdg…
Posted by VICTOR on August 10th, 2011.
Buy@Generic.Actos.15mg.30mg” rel=”nofollow”>..…
Buygeneric meds qmo…
Posted by CLIFTON on August 12th, 2011.
Order@Cheap.Coral.Calcium” rel=”nofollow”>..…
Buygeneric drugs lzt…
Posted by RODNEY on August 12th, 2011.
Order@Cheap.Abilify” rel=”nofollow”>……
Buywithout prescription xiu…
Posted by LLOYD on August 13th, 2011.
Abilify@5mg.10mg.15mg.20mg.30mg.Without.Prescription” rel=”nofollow”>……
Buygeneric meds svb…
Posted by PHILIP on August 14th, 2011.
Buy@Cheap.Acai” rel=”nofollow”>..…
Buyno prescription wkp…
Posted by ALFRED on August 14th, 2011.
Order@Acai.Online” rel=”nofollow”>..…
Buyno prescription jui…
Posted by BRUCE on August 14th, 2011.
Cheap@Acai.Online” rel=”nofollow”>……
Buyit now sge…
Posted by LEROY on August 14th, 2011.
Purchase@Cheap.Energy.Boost” rel=”nofollow”>.…
Buyit now ojb…
Posted by VINCENT on August 15th, 2011.
Buy@Cheap.Accutane” rel=”nofollow”>..…
Buyno prescription tao…
Posted by TRAVIS on August 16th, 2011.
Get@Accutane.Online” rel=”nofollow”>……
Buywithout prescription ccw…
Posted by JASON on August 17th, 2011.
Cheap@Generic.Accutane.10mg.20mg” rel=”nofollow”>……
Buynow it zdg…
Posted by BRUCE on August 17th, 2011.
Accutane@10mg.20mg.Without.Prescription” rel=”nofollow”>..…
Buygeneric drugs ajd…
Posted by RONNIE on August 18th, 2011.
Aciphex@20mg.Without.Prescription” rel=”nofollow”>.…
Buynow it lmg…
Posted by MARVIN on August 19th, 2011.
Buy@Discount.Acomplia” rel=”nofollow”>..…
Buydrugs without prescription faj…
Posted by MITCHELL on August 19th, 2011.
Cheap@Advair.Online” rel=”nofollow”>.…
Buygeneric drugs nih…
Posted by BRYAN on August 23rd, 2011.
Buy@Advair.25mcg50mcg.25mcg125mcg.25mcg250mcg.50mcg500mcg.50mcg100mcg.50mcg250mcg” rel=”nofollow”>..< /blo…
Buygeneric meds dxy…
Posted by ALVIN on August 24th, 2011.
Buy@Advair.25mcg50mcg.25mcg125mcg.25mcg250mcg.50mcg500mcg.50mcg100mcg.50mcg250mcg” rel=”nofollow”>…< /bl…
Buygeneric drugs vbr…
Posted by RUSSELL on August 24th, 2011.
< a href="http://trig.com/advair4746/biography/?ml=Generic-Advair-25mcg/50mcg-25mcg/125mcg-25mcg/250mcg-50mcg/500mcg-50mcg/100mcg-50mcg/250mcg-Without-Prescription Generic@Advair.25mcg50mcg.25mcg125mcg.25mcg250mcg.50mcg500mcg.50mcg100mcg.50mcg2…
Buydrugs without prescription jlg…
Posted by REGINALD on August 24th, 2011.
allopurinol@cheap.buy” rel=”nofollow”>..…
Buyit now…
Posted by JULIUS on August 27th, 2011.
Download@pop.Rock” rel=”nofollow”>……
Search rock US Charts…
Posted by greg on September 22nd, 2011.
warfarin zoloft…
Buy_drugs without prescription…
Posted by STEPHEN on October 17th, 2011.
do cats carry ringworm…
Buy_generic meds…
Posted by CHARLIE on October 17th, 2011.
recent advances in cancer breast…
Buy_now…
Posted by PATRICK on October 17th, 2011.
soft food diet ideas…
Buy_drugs without prescription…
Posted by ROGER on October 19th, 2011.
tuna during pregnancy…
Buy_generic meds…
Posted by MILTON on October 21st, 2011.
adult circumcisions and complications…
Buy_generic meds…
Posted by JORGE on October 24th, 2011.
gestational surrogacy…
Buy_now…
Posted by KEVIN on October 25th, 2011.
combination food diet…
Buy_drugs without prescription…
Posted by FREDDIE on October 25th, 2011.
buying prescription drugs in mexico…
Buy_now it…
Posted by ALLEN on October 26th, 2011.
faye sick lung cancer…
Buy_drugs without prescription…
Posted by WADE on October 26th, 2011.
blood pregnancy test jacksonville fl…
Buy_generic meds…
Posted by ANDRE on October 28th, 2011.
swollen ankles headache pregnancy…
Buy_generic drugs…
Posted by HARRY on October 29th, 2011.
questions about ortho tri cyclen…
Buy_generic drugs…
Posted by AUSTIN on November 2nd, 2011.
canine medi cam rx…
Buy_generic drugs…
Posted by DONALD on November 5th, 2011.
social interaction anxiety…
Buy_without prescription…
Posted by TERRY on November 8th, 2011.
at home diet programs…
Buy_generic pills…
Posted by RANDY on November 9th, 2011.
how can i lower my cholesterol…
Buy_generic drugs…
Posted by HARRY on November 11th, 2011.
foods with triglycerides in them…
Buy_generic drugs…
Posted by MARION on November 12th, 2011.
advisory group on hepatitis uk…
Buy_no prescription…
Posted by STEPHEN on November 13th, 2011.
flat warts and probiotics…
Buy_generic drugs…
Posted by DAN on November 15th, 2011.
medicare drugs covered…
Buy_generic meds…
Posted by ENRIQUE on November 16th, 2011.
state of arizona ontiveros drug…
Buy_now it…
Posted by VIRGIL on November 16th, 2011.
lexapro versus prozac differences…
Buy_without prescription…
Posted by ALFRED on November 16th, 2011.
sample ncp for diabetes…
Buy_without prescription…
Posted by SHANE on November 17th, 2011.
what year was advil introduced…
Buy_now it…
Posted by TRACY on November 17th, 2011.
Download@pop.Rock” rel=”nofollow”>.…
Buy rock US Charts…
Posted by jordan on December 8th, 2011.