Skip to content

Latest commit

 

History

History
177 lines (134 loc) · 3.07 KB

lecture3.mdx

File metadata and controls

177 lines (134 loc) · 3.07 KB

import { Head, Appear } from "mdx-deck"; import Logo from "./assets/images/logos/rdc-icon.svg"; export { default as theme } from "./theme"; import { CodeSurfer } from "mdx-deck-code-surfer"; import ultramin from "prism-react-renderer/themes/ultramin";

Welcome back

Remember to sit with your lab groups!


JavaScript functions

function add(a, b) {
  return a + b;
}

Defining a new function, with function


JavaScript functions

function doTwice(func, input) {
  func(func(input));
}

Taking a function as an argument



How JavaScript runs

Dealing with a chaos of inputs and outputs and staying efficient

<iframe src="https://giphy.com/embed/HSxKYjDLc9A6A" height="500" frameBorder="0" class="giphy-embed" allowFullScreen></iframe>


JavaScript doesn't sleep

setTimeout(function() {
  console.log("This will print after 5 seconds");
}, 5000);

console.log("But it won't wait for me!");

Function calls are processed like a queue!


Events

Using JavaScript to make our website interactive


Events

If this event happens, then do something

let btn = document.querySelector("button");
btn.addEventListener("<this event happens>", function(event) {
  // Do something
});

element" }, { tokens: { 2: [4] }, notes: "Specify the event name" }, { tokens: { 2: [7, 8, 9, 10, 12] }, lines: [3, 4], notes: "Specify what should happen when the event fires" } ]} />

What kinds of events can we listen for?

  • click
  • mousedown, mouseup, mousemove
  • input
  • keydown, keyup
  • touchstart, touchend, touchmove

Codealong!

https://tiny.cc/reactcookie


The Event Object

let btn = document.querySelector("button");
btn.addEventListener("click", function(event) {
  // What is `event`?
});

Codealong!

https://tiny.cc/reactgreeter