05.01.2013 Views

hide - Understanding jQuery

hide - Understanding jQuery

hide - Understanding jQuery

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Scope<br />

In order to understand function closures, one of the next subjects in the book, you must first<br />

have an understanding of the concept of scope. Scope is where things are happening in your<br />

program. Scope is usually determined by the { and } brackets. For example, the function “add”<br />

we discussed earlier uses these brackets to define its scope. In other words, when the function<br />

is executed, what is executed is the code within that scope.<br />

We can call the function twice or three times, and again, over and over, the code contained<br />

in the scope of that function will be executed. When a function is executed, a special part of<br />

memory is allocated into which the function is loaded. Then this code is executed.<br />

Unfortunately, the low-level details of computer program execution flow is not a part of this<br />

book, and you don’t really need to know them to be a JavaScript or <strong>jQuery</strong> programmer. But<br />

it’s an interesting subject you may want to explore on your own.<br />

The most important concept you do have to understand with regard to scope, is that variables<br />

in a scope are not visible to other scopes in a program.<br />

What does not visible mean?<br />

For example, variables defined inside a function scope only work within that function’s scope.<br />

You can’t work with them from within other functions. That’s why variables can be passed to<br />

functions as parameters, instead.<br />

Most of the time, you will not be able to recall variables across scopes. In other words, the<br />

scope the variable was created in, is the only scope it can be used in. But there is an exception,<br />

and that is when we use a function closure, a concept we will define in a few moments.<br />

Remember that all variables in the global scope are visible from everywhere. The global scope<br />

is not limited to any brackets, it exists at the root of the program, “above” all other scopes.<br />

Global Scope<br />

Global scope is the root of your program. What you define in the global scope, things like<br />

functions, variables, arrays, and so forth... will be visible from anywhere inside your program,<br />

including, within all defined functions.<br />

Also, on the contrary, variables defined inside a function cannot be visible in the global scope,<br />

they are only visible within that function. Use scope to your advantage to organize data of your<br />

program. Many programmers don’t even know that the concept of scope exists, they just follow<br />

their instinct. There is nothing wrong with that. But being aware of it can be important if<br />

you are looking to improve your understanding of writing code efficiently.<br />

19

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!