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

JavaScript Made Simple - WordCamp Miami 2017

JavaScript Made Simple - WordCamp Miami 2017

This talk opened the Learn JavaScript Deeply Track at WordCamp Miami. We break down some of the core JavaScript language basics and then get into the DOM and Events APIs.

Zac Gordon

March 26, 2017
Tweet

More Decks by Zac Gordon

Other Decks in Technology

Transcript

  1. WordCamp Miami 2017 It’s not that JavaScript is a hard

    or weird language. However, almost everything you do in JavaScript involves an API. That makes things more complicated. @zgordon
  2. JavaScript Made Simple 1. Language Basics 2. The DOM API

    3. DOM Events WordCamp Miami 2017 @zgordon
  3. Variable A container for storing values (in memory) WordCamp Miami

    2017 @zgordon var username = ‘zgordon’; let username = ‘zgordon’; const siteURL = ‘https://site.com’;
  4. Array A collection of values WordCamp Miami 2017 @zgordon let

    postIds = [1,2,3,5]; let usernames =[ 'zgordon', 'admin' ];
  5. Functions Let us write, call and reuse blocks of code

    WordCamp Miami 2017 @zgordon function getPosts() { let posts = apiMagic… // Will learn return posts; } getPosts();
  6. Function Parameters Can pass data into functions WordCamp Miami 2017

    @zgordon function getUser( userId ) { let user= apiMagic… // Will learn return user; } getUsers( 1 );
  7. Objects Container with properties (values) and methods (functions). WordCamp Miami

    2017 @zgordon let post = { id: 1, title: ‘Hello Object’, displayTitle: function() { console.log( this.title ); } } post.id; post.title; post.displayTitle();
  8. Loops Let us perform an action on a collection of

    items. WordCamp Miami 2017 @zgordon var postIds = [ 1, 7, 14, 34, 88, 117 ]; for ( let i = 0, max = postIds.length; i < max; i++ ) { console.log( 'Display post #' + postIds[i] ); }
  9. For Of Loop Lets us loop through an array WordCamp

    Miami 2017 @zgordon let postIds = [ 1, 2, 34, 55, 77 ]; for( let id of postIds ) { console.log( id ); }
  10. Conditional Statements Tests to determine what code to run WordCamp

    Miami 2017 @zgordon var loggedIn = true; if ( true === loggedIn ) { console.log( 'Show dashboard' ); } else { console.log( 'Please login' ); }
  11. Include JS in HTML WordCamp Miami 2017 @zgordon <html> <head>

    </head> <body> <script src="index.js"></script> </body> </html>
  12. Include JS in WP Theme WordCamp Miami 2017 @zgordon function

    theme_scripts() { wp_enqueue_script( 'main-js', get_template_directory_uri() . '/js/main.js', array( 'jquery' ), '1.1', true ); } add_action('wp_enqueue_scripts', ’theme_scripts', 999);
  13. JavaScript Made Simple 1. Language Basics 2. The DOM API

    3. DOM Events WordCamp Miami 2017 @zgordon
  14. WordCamp Miami 2017 Build documents, navigate their structure, and add,

    modify, or delete elements and content. @zgordon
  15. What to Know About the DOM 1. Nodes 2. Document

    Object Methods 3. Traversal (parents, children, siblings) 4. Creating, Appending (Cloning, Deleting) WordCamp Miami 2017 @zgordon
  16. Some of the Types of DOM Nodes • Document [9]

    • DocumentType [10] • Element [1] • Text [3] • Comments [8] • DocumentFragments [11] WordPress REST API
  17. 10 Doctype Root / HTML 1 Body 1 Head 1

    1 Title 3 Title Text 1 Header 1 Paragraph 3 Header Text 3 Text 1 Link 3 3 Link Text 9 Document WordCamp Miami 2017 @zgordon Window
  18. Styling Nodes WordCamp Miami 2017 @zgordon window.getComputedStyle( el ) el.style.backgroundColor

    = '#f7b733' el.classList.add( 'new' ) el.classList.remove( 'new' ) el.classList.toggle( 'new' )
  19. DOM Traversal WordCamp Miami 2017 @zgordon Any Node el.parentNode el.childNodes

    el.firstChild el.lastChild el.nextSibling el.previousSibling Element Nodes el.parentElement el.children el.firstElementChild el.lastElementChild el.nextElementSibling el.previousElementSibling el = document.querySelector('p');
  20. Appending Nodes WordCamp Miami 2017 @zgordon let p = document.querySelector('p');

    let a = document.createElement('a'); let text = document.createTextNode('Link text'); a.appendChild( text ); a.href = "https://link.com" p.appendChild( a );
  21. “Fake” Appending Nodes WordCamp Miami 2017 @zgordon let p =

    document.querySelector('p'); let markup = ''; markup += '<a href="https://site.com">'; markup += 'Link Text'; markup += '</a>'; p.innerHTML = markup;
  22. Cloning Nodes WordCamp Miami 2017 @zgordon let ul = document.querySelector('ul');

    let li = document.querySelector('li'); let newLi = li.cloneNode( true ); ul.appendChild( li );
  23. JavaScript Made Simple 1. Language Basics 2. The DOM API

    3. DOM Events WordCamp Miami 2017 @zgordon
  24. Types of Events WordCamp Miami 2017 @zgordon • Mouse events

    • Keyboard events • Form events • Media events • Drag and Drop events • Window events • Many more…. https://javascriptforwp.com/intro-to-events/
  25. Hook JavaScript into Events WordCamp Miami 2017 @zgordon 1.Inline 2.Global

    3.Listeners <a script="alert('hi')">Alert</a> a.onclick = sayHi a.addEventListener( 'click’, sayHi, false )
  26. Event Listeners WordCamp Miami 2017 @zgordon var linkEl = document.querySelector(

    'a' ); function displayLinkInfo( event ) { event.preventDefault(); console.log( event.target.innerHTML ); } linkEl.addEventListener( 'click', displayLinkInfo, false );
  27. The Event Object WordCamp Miami 2017 @zgordon link.addEventListener( 'click', sayHi,

    false ); function sayHi( event ) { // event is the event object console.log( event ); console.log( event.type ); console.log( event.target ); }
  28. WordCamp Miami 2017 @zgordon Event Propagation <html> <body> <p> <a>

    document window Phase 2 - Target <a> Phase 1 - Capturing
  29. WordCamp Miami 2017 @zgordon Event Propagation <html> <body> <p> <a>

    document window <a> Phase 1 - Capturing Phase 2 - Target Phase 3 - Bubbling
  30. WordCamp Miami 2017 @zgordon Event Propagation <html> <body> <p> <a>

    document window Phase 1 - Capturing Phase 2 - Target Phase 3 - Bubbling <a>
  31. JavaScript Made Simple 1. Language Basics 2. The DOM API

    3. DOM Events WordCamp Miami 2017 @zgordon
  32. WordCamp Miami 2017 It’s not that JavaScript is a hard

    or weird language. However, almost everything you do in JavaScript involves an API. That makes things more complicated. @zgordon