Skip to Content
Learning jQuery - Fourth Edition
book

Learning jQuery - Fourth Edition

by Jonathan Chaffer, Karl Swedberg
June 2013
Beginner content levelBeginner
444 pages
9h 45m
English
Packt Publishing
Content preview from Learning jQuery - Fourth Edition

Handling 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 outerFn() {
  var outerVar = 0;
  function innerFn1() {
    outerVar++;
    console.log('(1) outerVar = ' + outerVar);
  }
  function innerFn2() {
    outerVar += 2;
    console.log('(2) outerVar = ' + outerVar);
  }
  return {'fn1': innerFn1, 'fn2': innerFn2};
}
var fnRef = outerFn();
fnRef.fn1();
fnRef.fn2();
fnRef.fn1();
var fnRef2 = outerFn();
fnRef2.fn1();
fnRef2.fn2();
fnRef2.fn1();

Listing A.9

We return references to both functions, using an object to do so (this illustrates another way in which a reference to an inner ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning jQuery 3 - Fifth Edition

Learning jQuery 3 - Fifth Edition

Adam Boduch, Jonathan Chaffer, Karl Swedberg
jQuery Cookbook

jQuery Cookbook

Cody Lindley

Publisher Resources

ISBN: 9781782163145Other