Online Book Reader

Home Category

Getting Good with JavaScript - Andrew Burgess [13]

By Root 224 0
the current hour.

I said that for option 2 above, all the extra arguments are lost. Well, that's not exactly true. JavaScript does provides a way to get to them: it offers an object called arguments that you can access from inside your functions. You might think that it would make more sense if arguments was an array … and every other JavaScript programmer would agree with you. However, it's an array-like object which means it has some of the characteristics of an array, but not all of them. It has a length property, which is the number of arguments passed into the function. You're also able to access the parameters with square bracket notations, just like in an array. There are no other similarities with a real array. That is all.

So we could write a sum function like this: we loop over each argument that was passed in and add it to the variable sum. Then, we return sum.

Example 3.5


function sum() {

var sum = 0, i;

for ( i = 0 ; arguments[i]; i++) {

sum += arguments[i];

}

return sum;

}

var sum1 = sum(1, 2),

sum2 = sum(100, 75, 40),

sum3 = sum(9, 1.4, 8, 2, 79, 3234, 6, 4, 5e3, 5);

alert(sum1); // 2

alert(sum2); // 215

alert(sum3); // 8348.4

Scope

We mentioned scope in passing (quite) a few paragraphs ago. Let's discuss that in more detail now. While scope isn't related to functions only, functions are a main part of scope in JavaScript, so this is appropriate timing.

Scope is basically the set of variables that you have access to at a given point in your code. JavaScript has function scope, which means that the scope changes when we enter a function. Let's look at a few examples.

Let's start with the global area. If you write

var name = "Bob";


at the top level of a JavaScript file—meaning it's not inside a function—that variable will be accessible from everywhere within the JavaScript environment. Most often, that environment will be a webpage; this means that every other JavaScript file on the page will have access to that name variable from everywhere within the file. Any variables created in this "global namespace" will have global access-ability.

ROCKSTAR TIP

You might think that having access to variables globally is a great thing. With that, you don't ever have to worry about whether or not you can get to the value you need—because you always can! This is emphatically not the case . . . and we'll talk more about why that is in the next chapter.

As I mentioned, the scope changes when we enter a function, and only when we enter a function (this isn't true of most other languages). We still have access to all the variables outside the function, but now we have a group of variables that can be accessed from only inside this function.

For example,

Example 3.6


var name = "Bob";

function greet (greeting) {

var punc = "!!!";

return greeting + " " + name + punc;

}

alert( greet("Hello") ); // "Hello Bob!!!"


This rather simple example demonstrates function scope. The name variable is global, so we can reach it from inside the function. Arguments of the function, like greeting, are part of the function's scope, so there's no way to access that outside the function. Also part of the function's scope are any variables created inside the function: in this case, that's punc.

This will blow your mind: we can nest functions in JavaScript. Nested functions are just like their parent functions, in that they have access to all the scopes "above" them, as well as their own:

Example 3.7


var name = "Bob";

function greet(greeting) {

function get_punc() {

return (greeting === "Why") ? "?" : "!";

}

return greeting + " " + name + get_punc() + " " + greeting + get_punc();

}

alert( greet("Why") ); // "Why Bob? Why?"

alert( greet("Hi") ); // "Hi Bob! Hi!"


Another example that hopefully points out the scope of nested function; the inner function can access the greeting argument of its parent function, and if it needed to it could access name as well. As you might guess, a function inside a function is useful when you need to execute the same code more than once during the

Return Main Page Previous Page Next Page

®Online Book Reader