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

Understanding Javascript

Understanding Javascript

An overview of Javascript concepts:
Variable Scope
Variable Hoisting
Function Statement vs Expression
What is a function?
'this' context
Closures

Carlo Costantini

August 19, 2013
Tweet

More Decks by Carlo Costantini

Other Decks in Programming

Transcript

  1. Variable Scope Variable Hoisting Function Statement vs Expression What is

    a function? 'this' context Closures Tuesday, 20 August, 13
  2. var a = 10; var getA = function() { var

    a = 20; return a; }; > console.log(a); Tuesday, 20 August, 13
  3. var a = 10; var getA = function() { var

    a = 20; return a; }; > console.log(a); Tuesday, 20 August, 13
  4. var a = 10; var getA = function() { var

    a = 20; return a; }; > console.log(a); Tuesday, 20 August, 13
  5. var a = 10; var getA = function() { var

    a = 20; return a; }; > console.log(a); > 10 Tuesday, 20 August, 13
  6. var a = 10; var getA = function() { var

    a = 20; return a; }; getA(); > console.log(a); Tuesday, 20 August, 13
  7. var a = 10; var getA = function() { var

    a = 20; return a; }; getA(); > console.log(a); > 10 Tuesday, 20 August, 13
  8. var a = 10; var getA = function() { a

    = 20; return a; }; getA(); > console.log(a); Tuesday, 20 August, 13
  9. var a = 10; var getA = function() { a

    = 20; return a; }; getA(); > console.log(a); Tuesday, 20 August, 13
  10. var a = 10; var getA = function() { a

    = 20; return a; }; getA(); > console.log(a); Tuesday, 20 August, 13
  11. var a = 10; var getA = function() { a

    = 20; return a; }; getA(); > console.log(a); Tuesday, 20 August, 13
  12. var a = 10; var getA = function() { a

    = 20; return a; }; getA(); > console.log(a); > 20 Tuesday, 20 August, 13
  13. Scope is Function based. A variable is visible everywhere in

    the function it was defined but not outside of the function Tuesday, 20 August, 13
  14. var getA = function() { var a = 100; }

    getA(); console.log(a); Tuesday, 20 August, 13
  15. var getA = function() { var a = 100; }

    getA(); console.log(a); Tuesday, 20 August, 13
  16. var getA = function() { var a = 100; }

    getA(); console.log(a); > undefined Tuesday, 20 August, 13
  17. var getA = function() { console.log(a); var a = "some

    value"; } getA(); Tuesday, 20 August, 13
  18. var getA = function() { console.log(a); if (condition) { var

    a = "some value"; } } getA(); Tuesday, 20 August, 13
  19. var getA = function() { console.log(a); if (condition) { var

    a = "some value"; } } getA(); var getA = function() { console.log(a); var a = "some value"; } getA(); Tuesday, 20 August, 13
  20. var getA = function() { console.log(a); if (condition){ var a

    = "some value"; } } getA(); Tuesday, 20 August, 13
  21. var getA = function() { console.log(a); if (condition){ var a

    = "some value"; } } getA(); Tuesday, 20 August, 13
  22. var getA = function() { var a; //<--hoisting console.log(a); if

    (condition){ var a = "some value"; } } getA(); Tuesday, 20 August, 13
  23. var getA = function() { var a; console.log(a); if (condition){

    a = "some value"; } } getA(); Tuesday, 20 August, 13
  24. <?php $a = 10; function getA(){ echo $a; if ($condition){

    $a = "some value"; } } getA(); Tuesday, 20 August, 13
  25. <?php $a = 10; function getA(){ global $a; echo $a;

    if ($condition){ $a = "some value"; } } getA(); Tuesday, 20 August, 13
  26. var foo = 5; (function() { var bar = foo;

    if (1) { var foo = 10; } alert(bar); }()); Tuesday, 20 August, 13
  27. var foo = 5; (function() { var bar = foo;

    if (1) { var foo = 10; } alert(bar); }()); Tuesday, 20 August, 13
  28. var foo = 5; (function() { var bar = foo;

    if (1) { var foo = 10; } alert(bar); }()); Tuesday, 20 August, 13
  29. var foo = 5; (function() { var bar = foo;

    if (1) { var foo = 10; } alert(bar); }()); Tuesday, 20 August, 13
  30. var foo = 5; (function() { var bar = foo;

    if (1) { var foo = 10; } alert(bar); }()); Tuesday, 20 August, 13
  31. var foo = 5; (function() { var foo; var bar

    = foo; if (1) { var foo = 10; } alert(bar); }()); Tuesday, 20 August, 13
  32. if you start with function, it is a statement function

    Banana(){} var banana = function (){}; var banana = function Apple(){}; Tuesday, 20 August, 13
  33. if you start with function, it is a statement //

    statement function Banana(){} var banana = function (){}; var banana = function Apple(){}; Tuesday, 20 August, 13
  34. if you start with function, it is a statement function

    Banana(){} // expression var banana = function (){}; var banana = function Apple(){}; Tuesday, 20 August, 13
  35. if you start with function, it is a statement function

    Banana(){} var banana = function (){}; // named expression var banana = function Apple(){}; Tuesday, 20 August, 13
  36. If statements and function statements don't mix. The ECMAScript standard

    says that this should not be supported. However browsers handle it differently and always wrong. Tuesday, 20 August, 13
  37. Very bad, never do this: function conditionalFunc(){ if (condition) {

    function someFunc() { return foo; }; } else { function someFunc() { return bar; }; } return someFunc; }); Tuesday, 20 August, 13
  38. Better but far from good: function conditionalFunc() { var someFunc;

    if (condition){ someFunc = function (){ return foo; } } else { someFunc = function() { return bar; } } return someFunc; }; Tuesday, 20 August, 13
  39. Much better and probably best: function conditionalFunc() { var biz;

    if (condition) { biz = foo; } else { biz = bar; } return function(){ return biz; } }; Tuesday, 20 August, 13
  40. There are four ways functions can be called: 1. as

    a Method 2. as a function 3. as a Constructor 4. using call or apply Tuesday, 20 August, 13
  41. There are four ways functions can be called: 1. as

    a Method (obj.func();) 2. as a function 3. as a Constructor 4. using call or apply Tuesday, 20 August, 13
  42. There are four ways functions can be called: 1. as

    a Method 2. as a function (func();) 3. as a Constructor 4. using call or apply Tuesday, 20 August, 13
  43. There are four ways functions can be called: 1. as

    a Method 2. as a function 3. as a Constructor (new Func();) 4. using call or apply Tuesday, 20 August, 13
  44. There are four ways functions can be called: 1. as

    a Method 2. as a function 3. as a Constructor 4. using call or apply (func.apply();) Tuesday, 20 August, 13
  45. var someObj = { prop : ‘myproperty’, myMethod : function

    (){ console.log(this.prop); } } someObj.myMethod(); Tuesday, 20 August, 13
  46. var someObj = { prop : ‘myproperty’, myMethod : function

    (){ console.log(this.prop); } } someObj.myMethod(); > myproperty Tuesday, 20 August, 13
  47. SomeObj = function () { this.someproperty = “alpha”; } var

    myObj = new SomeObj(); console.log(myObj.someproperty); Tuesday, 20 August, 13
  48. SomeObj = function () { this.someproperty = “alpha”; } var

    myObj = new SomeObj(); console.log(myObj.someproperty); > alpha Tuesday, 20 August, 13
  49. var getA = function() { var a=20; return { value

    : a }; }; > console.log(new getA().value); Tuesday, 20 August, 13
  50. var getA = function() { var a=20; return { value

    : a }; }; > console.log((new getA()).value); Tuesday, 20 August, 13
  51. var getA = function() { var a=20; return { value

    : a }; }; > console.log((new getA()).value); > 20 Tuesday, 20 August, 13
  52. var getA = function() { this.value = 20; return 0;

    }; > console.log(new getA().value); Tuesday, 20 August, 13
  53. var getA = function() { this.value = 20; //return 0;

    return this; }; > console.log((new getA()).value); Tuesday, 20 August, 13
  54. var getA = function() { this.value = 20; //return 0;

    return this; }; > console.log((new getA()).value); > 20 Tuesday, 20 August, 13
  55. var getProperty = function () { return this.property; } var

    myObj = {‘property’ : ‘alpha’}; var prop = getProperty.apply(myObj); console.log(prop); Tuesday, 20 August, 13
  56. var getProperty = function () { return this.property; } var

    myObj = {‘property’ : ‘alpha’}; var prop = getProperty.apply(myObj); console.log(prop); Tuesday, 20 August, 13
  57. var getProperty = function () { return this.property; } var

    myObj = {‘property’ : ‘alpha’}; var prop = getProperty.apply(myObj); console.log(prop); > alpha Tuesday, 20 August, 13
  58. var setProperty = function () { var properties = {

    ‘alpha’ : ‘foo’, ‘beta‘ : ‘bar’ }; this.property = properties[arguments[0]]; } var myObj = {‘property’ : ‘’}; setProperty.apply(myObj,[‘alpha’]); console.log(myObj.property); Tuesday, 20 August, 13
  59. var setProperty = function () { var properties = {

    ‘alpha’ : ‘foo’, ‘beta‘ : ‘bar’ }; this.property = properties[arguments[0]]; } var myObj = {‘property’ : ‘’}; setProperty.apply(myObj,[‘alpha’]); console.log(myObj.property); Tuesday, 20 August, 13
  60. var setProperty = function () { var properties = {

    ‘alpha’ : ‘foo’, ‘beta‘ : ‘bar’ }; this.property = properties[arguments[0]]; } var myObj = {‘property’ : ‘’}; setProperty.apply(myObj,[‘alpha’]); console.log(myObj.property); >foo Tuesday, 20 August, 13
  61. var setProperty = function (prop) { var properties = {

    ‘alpha’ : ‘foo’, ‘beta‘ : ‘bar’ }; this.property = properties[prop]; } var myObj = {‘property’ : ‘’}; setProperty.call(myObj,‘alpha’); console.log(myObj.property); Tuesday, 20 August, 13
  62. var setProperty = function (prop) { var properties = {

    ‘alpha’ : ‘foo’, ‘beta‘ : ‘bar’ }; this.property = properties[prop]; } var myObj = {‘property’ : ‘’}; setProperty.call(myObj,‘alpha’); console.log(myObj.property); Tuesday, 20 August, 13
  63. var setProperty = function (prop) { var properties = {

    ‘alpha’ : ‘foo’, ‘beta‘ : ‘bar’ }; this.property = properties[prop]; } var myObj = {‘property’ : ‘’}; setProperty.call(myObj,‘alpha’); console.log(myObj.property); >foo Tuesday, 20 August, 13
  64. var f,getA = function() { var a=0; return function (){

    return a = a+1; }; }; f = getA(); console.log(f()); Tuesday, 20 August, 13
  65. var f,getA = function() { var a=0; return function (){

    return a = a+1; }; }; f = getA(); console.log(f()); Tuesday, 20 August, 13
  66. var f,getA = function() { var a=0; return function (){

    return a = a+1; }; }; f = getA(); console.log(f()); Tuesday, 20 August, 13
  67. var f,getA = function() { var a=0; return function (){

    return a = a+1; }; }; f = getA(); console.log(f()); Tuesday, 20 August, 13
  68. var f,getA = function() { var a=0; return function (){

    return a = a+1; }; }; f = getA(); console.log(f()); Tuesday, 20 August, 13
  69. var f,getA = function() { var a=0; return function (){

    return a = a+1; }; }; f = getA(); console.log(f()); Tuesday, 20 August, 13
  70. var f,getA = function() { var a=0; return { add:

    function () { return a = a+1; }, sub: function () { return a = a-1; } }; }; f = getA(); console.log(f.add()); console.log(f.sub()); Tuesday, 20 August, 13
  71. var getPeachTree = function (peaches) { var peaches = peaches

    || 10; var Peach = function () { var eaten = false; this.eat = function(){ if (!eaten) { eaten = true; console.log('mmm'); } else { console.log('all gone.'); } }; }; return { 'getPeach' : function () { if (peaches < 1){ console.log('no more peaches'); return null; } peaches--; return new Peach(); } }; }; Tuesday, 20 August, 13
  72. var getPeachTree = function (peaches) { var peaches = peaches

    || 10; var Peach = function () { var eaten = false; this.eat = function(){ if (!eaten) { eaten = true; console.log('mmm'); } else { console.log('all gone.'); } }; }; return { 'getPeach' : function () { if (peaches < 1){ console.log('no more peaches'); return null; } peaches--; return new Peach(); } }; }; Tuesday, 20 August, 13
  73. var ptree = getPeachTree(2); var peach = ptree.getPeach(); var peach2

    = ptree.getPeach(); peach.eat(); //mmm peach.eat(); //all gone peach2.eat(); //mmm peach2.eat(); //all gone var p3 = ptree.getPeach(); //no more peaches Tuesday, 20 August, 13
  74. What to Watch: Crockford on Javascript YUI Theatre YouTube channel

    http://www.youtube.com/playlist? list=PL7664379246A246CB&feature=plcp What to read: Javascript the Good Parts - D. Crockford Eloquent Javascript - M. Haverbeke Javascript Patterns - S. Stefanov High Performance Javascript - N. Zakas Maintainable Javascript - N. Zakas Tuesday, 20 August, 13