Closure and Scope

Intro

This code illustrates many different things, some of which are...

  • Closures.
  • Private properties.
  • Object methods as event listeners.
  • Scope of 'this' and other variables.
  • Using a destructor to prevent circular references in IE.
  • Static properties (where all instances share the same property).
  • And more...

The Code

var grp = new Array(); // Global

window.onload = function()
{
  grp[0] = new Group('div1', '*');
  grp[1] = new Group('div2', '*');
}

window.onunload = function()
{
  for (var i = 0; i < grp.length; ++i) {
    grp[i].onunload();
  }
}

// Group Object Prototype

function Group(sDivId, sTag)
{
  //// Private Properties
  
  var me = this;
  var ele = document.getElementById(sDivId);
  var eleList = ele.getElementsByTagName(sTag);
  
  //// Public Property
  
  this.instIndex = Group.instances.length;

  //// Constructor Code

  // Add this object to the instances array
  Group.instances[this.instIndex] = this;

  // Assign event listeners
  // to all sTag children of sDivId.
  for (var i = 0; i < eleList.length; ++i)
  {
    eleList[i].onclick = onClick;
    eleList[i].onmouseover = ClickGroup_onMouseover;
  }

  //// Event Listener (private)

  function onClick(evt)
  {
    // Within this event listener...
    // 'this' points to the Element object.
    // 'me' points to the Group instance object.
    // Group's private instance variables and
    // prototype parameters are in scope by closure.
    cMsg(this.nodeName + '.onclick, me.instIndex: ' + me.instIndex);
  }

  //// Public Methods

  this.foo = function()
  {
    // Within this method...
    // 'this' points to the Group instance object.
    // Group's private instance variables and
    // prototype parameters are in scope by closure.
  }

  this.onunload = function() // Destructor
  {
    // Remove circular references for IE
    for (var i = 0; eleList && i < eleList.length; ++i)
    {
      eleList[i].onmouseover = null;
    }  
    ele = null;
    eleList = null;
  }
} // end Group Object Prototype

//// Group Static Property

Group.instances = new Array();

//// Event Listener (global)

function ClickGroup_onMouseover(evt)
{
  // Within this event listener...
  // 'this' points to the Element object.
  // Group's members are not in scope.

  mMsg(this.nodeName + '.onmouseover');
}

//// Debug Messages
var cCnt = 0;
function cMsg(msg){document.getElementById('cTA').value += ++cCnt + ': ' + msg + '\n';}
var mCnt = 0;
function mMsg(msg){document.getElementById('mTA').value += ++mCnt + ': ' + msg + '\n';}

HTML for Experimenting

DIV 1

div1
***

H5-Target Content: Lorem ipsum dolor sit amet, consectetaur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

H5-Trigger

H5-Target Content: Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

div1

Here comes two H5s...

H5-Trigger

H5-Target Content: Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Click Messages

Mouseover Messages

DIV 2

div2

H4-Target Content: Lorem ipsum dolor sit amet, consectetaur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

H4-Trigger

H4-Target Content: Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

-Tr**r

get Content: Lorem ipsum dolor sit amet, consectetaur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Related demo

Closure & Memory-Leak Resources

About Cross-Browser.com

Cross-Browser.com is the home of X - a cross-browser Javascript library, and many demos, applications, articles and documentation.

Search

Cross-Browser.com

World Wide Web