Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Scope and Closures

Scope and Closures

An explanation of scope and closure intricacies.

Rob Taylor

April 17, 2013
Tweet

More Decks by Rob Taylor

Other Decks in Programming

Transcript

  1. (function () { var myVar = "My String"; (function ()

    { myVar = "My String, Rewritten"; }()); alert(myVar); }()); Alert: "My String, Rewritten"
  2. (function () { (function () { var myVar = "My

    String"; }()); alert(myVar); }());
  3. (function () { (function () { var myVar = "My

    String"; }()); alert(myVar); }()); Alert: Undefined
  4. (function () { var myVar = "My String"; var myFunction

    = function () { myVar = "The String, Rewritten"; }; alert(myVar); }());
  5. (function () { var myVar = "My String"; var myFunction

    = function () { myVar = "The String, Rewritten"; }; alert(myVar); }()); Alert: "My String"
  6. (function () { var myVar = "My String"; var myFunction

    = function () { myVar = "The String, Rewritten"; }; alert(myVar); myFunction(); }());
  7. (function () { var myVar = "My String"; var myFunction

    = function () { myVar = "The String, Rewritten"; }; alert(myVar); myFunction(); }()); Alert: "My String"
  8. (function () { var myVar = "My String"; var myFunction

    = function () { myVar = "The String, Rewritten"; }; myFunction(); alert(myVar); }());
  9. (function () { var myVar = "My String"; var myFunction

    = function () { myVar = "The String, Rewritten"; }; myFunction(); alert(myVar); }()); Alert: "The String, Rewritten"
  10. (function () { var myVar; for (var i = 1;

    i <= 20; ++i) { document.getElementById("el-" + i).addEventListener("click", function () { myVar = "String #" + i; alert(myVar); }, false); } }());
  11. (function () { var myVar; for (var i = 1;

    i <= 20; ++i) { document.getElementById("el-" + i).addEventListener("click", function () { myVar = "String #" + i; alert(myVar); }, false); } }()); Click #20 > Alert: "String #20"
  12. (function () { var myVar; for (var i = 1;

    i <= 20; ++i) { document.getElementById("el-" + i).addEventListener("click", function () { myVar = "String #" + i; alert(myVar); }, false); } }()); Click #10 > Alert: "String #20"
  13. (function () { var myVar; for (var i = 1;

    i <= 20; ++i) { document.getElementById("el-" + i).addEventListener("click", function () { myVar = "String #" + i; alert(myVar); }, false); } }()); Click #1 > Alert: "String #20"
  14. (function () { var myVar = "My String"; (function ()

    { var myVar = "My String, Rewritten"; }()); }());
  15. (function () { var myVar = "My String"; alert(myVar); (function

    () { var myVar = "My String, Rewritten"; }()); }());
  16. (function () { var myVar = "My String"; alert(myVar); (function

    () { var myVar = "My String, Rewritten"; }()); }()); Alert: "My String"
  17. (function () { var myVar = "My String"; (function ()

    { alert(myVar); var myVar = "My String, Rewritten"; }()); }());
  18. (function () { var myVar = "My String"; (function ()

    { alert(myVar); var myVar = "My String, Rewritten"; }()); }()); Alert: Undefined
  19. (function () { var myVar = "My String"; (function ()

    { var myVar = "My String, Rewritten"; alert(myVar); }()); }());
  20. (function () { var myVar = "My String"; (function ()

    { var myVar = "My String, Rewritten"; alert(myVar); }()); }()); Alert: "My String, Rewritten"
  21. (function () { var myVar = "My String"; (function ()

    { var myVar = "My String, Rewritten"; }()); alert(myVar); }());
  22. (function () { var myVar = "My String"; (function ()

    { var myVar = "My String, Rewritten"; }()); alert(myVar); }()); Alert: "My String"
  23. (function () { var myVar = "My String"; (function ()

    { var myVar = "My String, Rewritten"; }()); (function () { alert(myVar); }()); }());
  24. (function () { var myVar = "My String"; (function ()

    { var myVar = "My String, Rewritten"; }()); (function () { alert(myVar); }()); }()); Alert: "My String"
  25. (function () { var myVar = "My String"; (function ()

    { var myVar = "My String, Rewritten"; }()); (function () { alert(myVar); }()); }());
  26. var Person = function () { var name = "Steve";

    return { getName: function () { return name; }, setName: function (newName) { name = newName; } }; }; var personOne = new Person(); alert(personOne.name); Alert: Undefined
  27. var Person = function () { var name = "Steve";

    return { getName: function () { return name; }, setName: function (newName) { name = newName; } }; }; var personOne = new Person(); alert(personOne.getName()); Alert: "Steve"
  28. var Person = function () { var name = "Steve";

    return { getName: function () { return name; }, setName: function (newName) { name = newName; } }; }; var personOne = new Person(); personOne.setName("Rob"); var personTwo = new Person(); personTwo.setName("Hannah"); alert(personOne.getName()); Alert: "Rob"
  29. var Person = function () { var name = "Steve";

    return { getName: function () { return name; }, setName: function (newName) { name = newName; } }; }; var personOne = new Person(); personOne.setName("Rob"); var personTwo = new Person(); personTwo.setName("Hannah"); alert(personTwo.getName()); Alert: "Hannah"
  30. var Person = function () { this.name = "Steve"; };

    Person.prototype = { getName: function () { return name; }, setName: function (newName) { name = newName; } }; var personOne = new Person(); personOne.setName("Rob"); alert(personOne.getName()); Alert: "Rob"
  31. var Person = function () { this.familyName = "Anderson"; };

    Person.prototype = { setFamilyName: function (newFamilyName) { name = newFamilyName; }, mother: { firstName: "Susan", getFamilyName: function () { // ??? } } }; var personOne = new Person(); personOne.setFamilyName("Taylor");
  32. var Person = function () { this.familyName = "Anderson"; };

    Person.prototype = { setFamilyName: function (newFamilyName) { name = newFamilyName; }, mother: { firstName: "Susan", getFamilyName: function () { // ??? } } }; var personOne = new Person(); personOne.setFamilyName("Taylor");
  33. var personOne = { name: "Steve" }; var personTwo =

    { name: "Hannah" }; var sayHello = function (aPerson) { alert("Hello " + aPerson.name + ", my name is " + this.name); }; sayHello.call(personOne, personTwo); Alert: "Hello Hannah, my name is Steve"
  34. var personOne = { name: "Steve" }; var personTwo =

    { name: "Hannah" }; var sayHello = function (aPerson) { alert("Hello " + aPerson.name + ", my name is " + this.name); }; sayHello.call(personTwo, personOne); Alert: "Hello Steve, my name is Hannah"
  35. var personOne = { name: "Steve" }; var personTwo =

    { name: "Hannah" }; var sayHello = function (aPerson) { alert("Hello " + aPerson.name + ", my name is " + this.name); }; sayHello.apply(personOne, [ personTwo ]); Alert: "Hello Hannah, my name is Steve"
  36. var personOne = { name: "Steve" }; var personTwo =

    { name: "Hannah" }; var sayHello = function (aPerson) { alert("Hello " + aPerson.name + ", my name is " + this.name); }; sayHello.apply(personTwo, [ personOne ]); Alert: "Hello Steve, my name is Hannah"
  37. var Person = function () { this.name = "Steve"; };

    Person.prototype = { sayDelayedHello: function (delay) { setTimeout(this.sayHello, delay); }, sayHello: function () { alert("Hello, my name is " + this.name); } }; var person = new Person(); person.sayDelayedHello(1000);
  38. var Person = function () { this.name = "Steve"; };

    Person.prototype = { sayDelayedHello: function (delay) { setTimeout(this.sayHello, delay); }, sayHello: function () { alert("Hello, my name is " + this.name); } }; var person = new Person(); person.sayDelayedHello(1000); Alert: "Hello, my name is "
  39. var bind = function(callback, scope) { return function() { var

    args = Array.prototype.concat.call(arguments); callback.apply(scope, args); }; } var Person = function() { this.name = "Steve"; }; Person.prototype = { sayDelayedHello: function(delay) { setTimeout(bind(this.sayHello, this), delay); }, sayHello: function() { alert("Hello, my name is " + this.name); } }; var person = new Person(); person.sayDelayedHello(1000);
  40. var bind = function(callback, scope) { return function() { var

    args = Array.prototype.concat.call(arguments); callback.apply(scope, args); }; } var Person = function() { this.name = "Steve"; }; Person.prototype = { sayDelayedHello: function(delay) { setTimeout(bind(this.sayHello, this), delay); }, sayHello: function() { alert("Hello, my name is " + this.name); } }; var person = new Person(); person.sayDelayedHello(1000); Alert: "Hello, my name is Steve"