Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning  jQuery : Better Interaction Design and Web Development with Simple JavaScript Techniques

You're reading from   Learning jQuery : Better Interaction Design and Web Development with Simple JavaScript Techniques Better Interaction Design and Web Development with Simple JavaScript Techniques

Arrow left icon
Product type Paperback
Published in Jul 2007
Publisher Packt
ISBN-13 9781847192509
Length 380 pages
Edition Edition
Languages
Tools
Arrow right icon
Toc

Table of Contents (18) Chapters Close

Learning jQuery
Credits
About the Authors
About the Reviewers
Preface
1. Getting Started FREE CHAPTER 2. Selectors—How to Get Anything You Want 3. Events—How to Pull the Trigger 4. Effects—How to Add Flair to Your Actions 5. DOM Manipulation—How to Change Your Page on Command 6. AJAX—How to Make Your Site Buzzword-Compliant 7. Table Manipulation 8. Forms with Function 9. Shufflers and Rotators 10. Plug-ins 1. Online Resources 2. Development Tools 3. JavaScript Closures

Variable Scoping


Inner functions can of course have their own variables, which are restricted in scope to the function itself:

function outerFun() {
  function innerFun() {
    var innerVar = 0;
    innerVar++;
    alert(innerVar);
  }
  return innerFun;
}

Each time the function is called, through a reference or otherwise, a new variable innerVar is created, incremented, and displayed:

var globVar = outerFun();
globVar(); // Alerts "1"
globVar(); // Alerts "1"
var innerVar2 = outerFun();
innerVar2(); // Alerts "1"
innerVar2(); // Alerts "1"

Inner functions can reference global variables, in the same way as any other function can:

var globVar = 0;
function outerFun() {
  function innerFun() {
    globVar++;
    alert(globVar);
  }
  return innerFun;
}

Now our function will consistently increment the variable with each call:

var globVar = outerFun();
globVar(); // Alerts "1"
globVar(); // Alerts "2"
var globVar2 = outerFun();
globVar2(); // Alerts "3"
globVar2(); // Alerts "4"

But what if the variable is local to the parent function? Since the inner function inherits its parent’s scope, this variable can be referenced too:

function outerFun() {
  var outerVar = 0;
  function innerFun() {
    outerVar++;
    alert(outerVar);
  }
  return innerFun;
}

Now our function calls have more interesting behavior:

var globVar = outerFun();
globVar(); // Alerts "1"
globVar(); // Alerts "2"
var globVar2 = outerFun();
globVar2(); // Alerts "1"
globVar2(); // Alerts "2"

We get a mix of the two earlier effects. The calls to innerFun() through each reference increment innerVar independently. Note that the second call to outerFun() is not resetting the value of innerVar, but rather creating a new instance of innerVar, bound to the scope of the second function call. The upshot of this is that after the above calls, another call to globVar() will alert 3, and a subsequent call to globVar2() will also alert 3. The two counters are completely separate.

When a reference to an inner function finds its way outside of the scope in which the function was defined, this creates a closure on that function. We call variables that are not local to the inner function free variables, and the environment of the outer function call closes them. Essentially, the fact that the function refers to a local variable in the outer function grants the variable a stay of execution. The memory is not released when the function completes, as it is still needed by the closure.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image