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

07 - Javascript - OpenWebSchool

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

07 - Javascript - OpenWebSchool

Avatar for openwebschool

openwebschool

August 16, 2012
Tweet

More Decks by openwebschool

Other Decks in Programming

Transcript

  1. “I had to be done in ten days or something

    worse than JS would have happened”
  2. var a = 4; // 4 a = banana; //

    undefined a = 6; // 6
  3. 42

  4. ‘hello’ + ‘ world’ // ‘hello world’ ‘hello’ + 42

    // ‘hello42’ 42 + ‘hello’ // ‘42hello’
  5. 42 == 42 // true 42 != 42 // false

    42 == ’42’ // true
  6. [1, 2, 3, 4].length // 4 [1, 2, 3, 4][0]

    // 1 [1, 2, 3, 4][99] // undefined
  7. var a = [1, 2, 3, 4]; a[2] = ‘banana’;

    // [1, ‘banana’, 3, 4]
  8. var a = [1, 2, 3, 4]; typeof a; //

    ‘object’ Array.isArray(a); // true
  9. var a = [1, 5, 2, 4].pop(); // 4 //

    a === [1, 5, 2] a.sort(); // [1, 2, 5]
  10. { name: ‘Bob’, age: 4 }.name; // ‘Bob’ { name:

    ‘Bob’, age: 4 }[‘name’]; // ‘Bob’ { name: ‘Bob’, age: 4 }.banana; // undefined
  11. var obj = { name: ‘Bob’, age: 4 }; obj.name

    = ‘Alice’; // obj === { name: ‘Alice’, age: 4 } obj[‘name’] = ‘Alice’; // obj === { name: ‘Alice’, age: 4 }
  12. var obj = { name: ‘Bob’, age: 4 }; delete

    obj.name; // obj === { age: 4 }
  13. var obj = { name: ‘Bob’, age: 4 }; delete

    obj.name; // obj === { age: 4 }
  14. var two = function () { return 2; }; two;

    // function () { return 2; } two(); // 2
  15. var cakeRecipe = function (ingredient) { return ingredient + ‘

    cake’; }; var cookieRecipe = function (ingredient) { return ingredient + ‘ cookie’; }; cookieRecipe(‘chocolate’); // chocolate cookie
  16. var cook = function (recipe, ingredient) { return recipe(ingredient); };

    cook(cakeRecipe, ‘chocolate’); // chocolate cake cook(cookieRecipe, ‘chocolate’); // chocolate cookie
  17. var parser = function (base) { return function (x) {

    return parseInt(x, base); }; }; var parseBin = parser(2); parseBin(‘1010’); // 10
  18. var f = function () { return arguments[1]; }; f(2,

    3, true, { foo: ‘bar’ }); // 3
  19. var obj = { name: ‘Bob’, age: 4 }; ‘name’

    in obj; // true ‘banana’ in obj; // false
  20. var f = function () { var a = 4;

    }; f(); a; // undefined
  21. var a = [1, 2, 3]; for (var i =

    0, len = a.length; i < len; i++) { a[i]; // 1, 2, 3 ... }
  22. var a = [1, 2, 3]; var b = a.map(function

    (n) { return n * 2; }); b; // [2, 4, 6]
  23. var add = function (a, b) { return a +

    b; }; [1, 2, 3].reduce(add, 0); // 6