All pastes #964848 Raw Edit

JavaScript Closure Example

public javascript v1 · immutable
#964848 ·published 2008-03-31 16:28 UTC
rendered paste body
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><HTML><HEAD><TITLE>JavaScript Closure Example</TITLE><META HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=utf-8"></HEAD><SCRIPT TYPE="text/javascript">var closure = ( function() {  var closureVariable  var closure = function()  {   this.closureVariableCopy = closureVariable;   this.objectVariable = 0;   this.setclosureVariable = function(arg1)   {    closureVariable = arg1;   }   this.getclosureVariable = function()   {    return closureVariable;   }  }  return closure; })();closure1 = new closure();alert(typeof closure1);closure1.setclosureVariable(2);alert(closure1.getclosureVariable());closure2 = new closure();alert(closure2.getclosureVariable());alert('Thus, closure variables are shared among instances.');</SCRIPT><BODY></BODY></HTML>