2 Despite a syntax that suggests block scopefor example the body of a for loop

2 despite a syntax that suggests block scopefor

This preview shows page 246 - 248 out of 517 pages.

2. Despite a syntax that suggests block scope—for example, the body of a for-loop inside a function gets its own set of curly braces inside which additional var declarations can appear— all variables declared with var in a function are visible everywhere throughout that function, including to any nested functions. Hence, in a common construction such as for (var m in movieList) , the scope of m is the entire function in which the for-loop appears, not just the body of the for-loop itself. The same is true for variables declared with var inside the loop body. This behavior, called function scope , was invented in Algol 60. Keeping functions short (remember SOFA from Section 9.5 ?) helps avoid the pitfall of block vs. function scope. 3. An Array is really just a object whose keys are nonnegative integers. In some JavaScript implementations, retrieving an item from a linear array is marginally faster than retrieving an item from a hash, but not enough to matter in most cases. The pitfall is that if you try to index an array with a number that is negative or not an integer, a string-valued key will be created. That is,
Image of page 246
a[2.1] becomes a[”2.1”] . 4. The comparison operators == and != perform type conversions automatically, so ’5’==5.0 is true. The operators === and !== perform comparisons without doing any conversions. This is potentially confusing because Ruby also has a === (“threequal”) operator that does something quite different. 5. Equality for arrays and hashes is based on identity and not value, so [1,2,3]==[1,2,3] is false. Unlike Ruby, in which the Array class can define its own == operator, in JavaScript you must work around these built-in behaviors, because == is part of the language. 6. Strings are immutable, so methods like toUpperCase() always return a new object. Hence write s=s.toUpperCase() if you want to replace the value of an existing variable. 7. If you call a function with more arguments than its definition specifies, the extra arguments are ignored; if you call it with fewer, the unassigned arguments are undefined . In either case, the array arguments[] (within the function’s scope) gives access to all arguments that were actually passed. 8. String literals behave differently from strings created with new String if you try to create new properties on them, as the code excerpt below shows. The reason is that JavaScript creates a temporary “wrapper object” around fake to respond to fake.newprop=1 , performs the assignment, then immediately destroys the wrapper object, leaving the “real” fake without any newprop property. You can set extra properties on strings if you create them explicitly with new . But better yet, don’t set properties on built-in types: define your own prototype object and use composition rather than inheritance (Chapter 11 ) to make a string one of its properties, then set the other properties as you see fit. (This restriction applies equally to numbers and Booleans for the same reasons, but it doesn’t apply to arrays because, as we mentioned earlier, they are just a special case of hashes.) 1 real = new String("foo"); 2 fake = "foo"; 3 real.newprop
Image of page 247
Image of page 248

You've reached the end of your free preview.

Want to read all 517 pages?

  • Spring '19
  • Dr.Marcos

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors