Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Learning  jQuery : Better Interaction Design and Web Development with Simple JavaScript Techniques
  • Table Of Contents Toc
  • Feedback & Rating feedback
Learning  jQuery : Better Interaction Design and Web Development with Simple JavaScript Techniques

Learning jQuery : Better Interaction Design and Web Development with Simple JavaScript Techniques

close
close
Learning  jQuery : Better Interaction Design and Web Development with Simple JavaScript Techniques

Learning jQuery : Better Interaction Design and Web Development with Simple JavaScript Techniques

Overview of this book

Table of Contents (18 chapters)
close
close
Learning jQuery
Credits
About the Authors
About the Reviewers
Preface

Interactions between Closures


When more than one inner function exists, closures can have effects that are not as easy to anticipate. Suppose we pair our incrementing function with another function, this time incrementing by two:

function outerFun() {
  var outerVar = 0;
  function innerFun() {
    outerVar++;
    alert(outerVar);
  }
  function innerFun2() {
    outerVar = outerVar + 2;
    alert(globVar);
  }
  return {'innerFun': innerFun, 'outerFun2': outerFun2};
}

We return references to both functions, using a map to do so (this illustrates another way in which reference to an inner function can escape its parent). Both functions can be called through the references:

var globVar = outerFun();
globVar.innerFun(); // Alerts "1"
globVar.innerFun2(); // Alerts "3"
globVar.innerFun(); // Alerts "4"
var globVar2 = outerFun();
globVar2.innerFun(); // Alerts "1"
globVar2.innerFun2(); // Alerts "3"
globVar2.innerFun(); // Alerts "4"

The two inner functions refer to the same local variable, so they share the same closing environment. When innerFun() increments outerVar by 1, this sets the new starting value of outerVar when innerFun2() is called. Once again, though, we see that a subsequent call to outerFun() creates new instances of these closures with a new closing environment to match. Fans of object-oriented programming will note that we have in essence created a new object, with the free variables acting as instance variables and the closures acting as instance methods. The variables are also private, as they cannot be directly referenced outside of their enclosing scope, enabling true object-oriented data privacy.

Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Learning  jQuery : Better Interaction Design and Web Development with Simple JavaScript Techniques
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon