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

JavaScript Basics

Edd S
April 18, 2012

JavaScript Basics

Slides for an internal talk I gave while at MintDigital

Edd S

April 18, 2012
Tweet

More Decks by Edd S

Other Decks in Programming

Transcript

  1. function sit(seat){ sitting = ‘sitting in the ’+seat+‘ seat’; alert(sitting);

    } sit(‘front’); alert(sitting); sitting was set globally
  2. var weekDays = [ ‘monday’, ‘tuesday’ ... ]; var weekDays

    = new Array(‘monday’, ‘tuesday’ ... ); creating arrays
  3. var seat = { position: ‘front’, activity: ‘kicking’ }; var

    seat = new Object(); seat.position = ‘front’; seat.activity = ‘kicking’; creating objects
  4. var friday = {}; if( friday.now ) { // }

    undefined attributes don’t
  5. (function($){ // code that uses jQuery $ here }(jQuery)); pass

    through variables into anonymous functions
  6. var $car = $(‘div.car’), $frontSeat = $(‘p.front-seat’, $car); or $frontSeat

    = $car.children(‘p.front-seat’); scoped selectors
  7. <body id=“search-index”> <form method=“get” action=“” id=“search”> <input type=“text” name=“q”> <button

    type=“submit”>Search!</button> </form> <div id=“results”> </div> </body> a search page
  8. { results: [ { title: ‘fun’, url: ‘/fun’ }, {

    title: ‘excited’, url: ‘/excited’ }, ... ] } json response
  9. <body id=“search-index”> <form method=“get” action=“” id=“search”> <input type=“text” name=“q”> <button

    type=“submit”>Search!</button> </form> <div id=“results”> </div> </body> recap search page
  10. BB.bodyID(‘search-index’, function(){ var $form = $(‘form#search’), $results = $(‘div#results’); BB.submitInline($form,

    { success: function(json){ $.each(json.results, function(i, data){ $results.append(‘<a href=“’+data.url +‘”>’+data.title+‘</a>’); }); } } });
  11. function whichSeatShouldITake(){ var seats = [ ‘Front Left’, ‘Front Right’,

    ‘Back Left’, ‘Back Right’, ‘Back Middle’], random = Math.round(Math.random()*seats.length)); return “Just sit in the ” + seats[random] + “ seat and shut up”; }