Jump to content

Closure


davej

Recommended Posts

Could the following be considered a proper example of anonymous and named closure?

<!DOCTYPE html><html><head><title>Closure and Scope</title><script>var count = 999;var fnabc;var out0;var out1;var out2;window.onload = function() {document.getElementById('btn0').onclick = global;document.getElementById('btn1').onclick = step;fnabc = abc();out0 = document.getElementById('out0');out1 = document.getElementById('out1');out2 = document.getElementById('out2');document.getElementById('btn2').onclick = (function() { var count = 0; return function(e) { count++;out2.innerHTML = out2.innerHTML + ' ' + count;}})();}function global() {out0.innerHTML = out0.innerHTML + ' ' + count}function step() {out1.innerHTML = out1.innerHTML + ' ' + fnabc();}function abc() {var count = 0;    return function(e) {count++;return count;}}</script></head><body><p>Closure?</p><input type="button" id="btn0" value="Global"/><input type="button" id="btn1" value="Closure1"/><input type="button" id="btn2" value="Closure2"/><div id="out0"></div><div id="out1"></div><div id="out2"></div></body></html>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...