Chapter 2. Essentials Flashcards

1
Q

Why should you write maintainable code?

A

You should work to lessen the reading and comprehension time of your code.

Work to enhance code portability.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are some qualities of maintainable code?

A
  • Readable
  • Consistent
  • Predictable
  • Appears to be written by same person
  • Is Documented
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What’s the difference between a local and a global variable?

A

A global is declared outside of any funciton, or simply used without being declared.

A local variable is one declared inside a function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why should global variables be avoided?

A
  • Potenital for collisions: globals share the same namespace.
  • It’s too easy to use a variable without declaring it, therefore creating an implied global.
  • Portability: your code could be used in another environment and overwrite some host object.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What happens to a and b if this chained assignment is inside of a function?

var a = b = 0;

A

Javascript is evaluated right to left.

var a = (b = 0);

“b” becomes an implied global since it’s not declared; it’s value is set to zero.

“a” is a local variable and it’s value is set to zero.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
var global_day = new Date(2013, 04, 21);

    delete global_day;

What will the last line return?

A

False.

Globals created with the var keyword cannot be deleted. Implied globals are just properties of the global object, and can be deleted.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

In the browser environment, which identifier do you use to access the global object?

A

window

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Why should you declare all your variables at once at the top of a function?

A
  • Less physical code
  • Requires planning, minimizes globals
  • Provides single place to check initial var assignments.
  • Avoids logical errors due to hoisting.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What happens when you use a local variable inside a function before you declare it?

A

All declarations are “hoisted” to the top of the function, where they are assigned “undefined” until they are declared.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How can you optimize the common for loop?

for (var i = 0; i < my_array.length; i++ {
      //do something
 )
A
  • Each time you access a collection, like an array of elements, you have to live query the DOM. You should cache the length.
  • i ++ should be converted to ** i += 1** for legibility.
  • Move var out to own line for declarations.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Name the three getElementsBy… DOM methods that return HTML Collections.

A
  1. document.getElementsByName()
  2. document.getElementsByClassName()
  3. document.getElementsByTagName()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Name four HTML Collections found on the document object.

A
  1. document.images
  2. document.links
  3. document.forms
  4. document.forms[index].elements
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the optimized for loop?

A
var i, myarray = []; for (i = myarray.length; i -= 1;) { //do stuff }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the optimized while loop?

A
var myarray = [], i = myarray.length; while (i -= 1) { //do stuff }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What general term is also known as using a for-in loop?

A

Enumeration occurs when you iterate over each part of an object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the general rule for enumerating in JavaScript?

A

for loop enumerates arrays

for-in loop enumerates objects

17
Q

Why shouldn’t you use a for-in loop to enumerate an array?

A

Because arrays are objects, you will get properties and methods found on the array object, not just it’s members.

18
Q

How do you filter out inherited items during enumeration?

A
if (someObject.hasOwnProperty(i)) {     //continue. }
19
Q
A