Skip to content
This repository was archived by the owner on May 14, 2024. It is now read-only.

Commit d631870

Browse files
Gabe Rodriguezkvarak
authored andcommitted
Adding week 3 lecture notes
1 parent 7d6c81b commit d631870

File tree

4 files changed

+110
-1
lines changed

4 files changed

+110
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Here you can find course content and homework for the JavaScript2 module
99
|----|-----|----|--------|--------|
1010
|1.|• Capturing user input through forms <br>• [Events](http://javascript.info/introduction-browser-events)<br>• [Basic DOM manipulations](../../../fundamentals/blob/master/fundamentals/DOM_manipulation.md)<br>• [Code debugging using the browser](http://javascript.info/debugging-chrome) <br>• [Code commenting](../../../fundamentals/blob/master/fundamentals/code_commenting.md)<br>• Structuring code files<br>• [Code formatting](../../../fundamentals/blob/master/fundamentals/code_formatting.md)<br>• [Handing in homework via PR](../../..//fundamentals/blob/master/fundamentals/homework_pr.md) |[Reading Week 1](/Week1/README.md)|[Homework Week 1](/Week1/MAKEME.md)|
1111
|2.|• Functions + JSON/Arrays<br>• [Array Manipulations](../../../fundamentals/blob/master/fundamentals/array_manipulation.md)<br>• JSON<br>• [Map and filter](../../../fundamentals/blob/master/fundamentals/map_filter.md)<br>• Arrow functions |[Reading Week 2](/Week2/README.md)|[Homework Week 2](/Week2/MAKEME.md)|[Notes](/Week2/LECTURENOTES.md)
12-
|3.|[Closures](../../../fundamentals/blob/master/fundamentals/scope_closures_this.md) <br>• Callbacks|[Reading Week 3](/Week3/README.md)|[Homework Week 3](/Week3/MAKEME.md)|
12+
|3.|• [Closures](../../../fundamentals/blob/master/fundamentals/scope_closures_this.md) <br>• Callbacks|[Reading Week 3](/Week3/README.md)|[Homework Week 3](/Week3/MAKEME.md)|[Notes](/Week3/LECTURENOTES.md)
1313

1414
## Test
1515
At the end of this module you'll be doing a formative test. It will be done on **paper** and will consist of **4 exercises** that will test your JavaScript1 and JavaScript2 knowledge.

Week3/LECTURENOTES.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
### Lecture Notes Week 4
2+
3+
## Reviewing
4+
5+
Cover individual topics that students may still be stuck on from previous lectures and homework
6+
7+
## Callbacks
8+
9+
- How do DOM event listeners work?
10+
- Can reference color-change-example.html
11+
- One INEFFICIENT way would be to actually have each element listening/polling every second whether is has been clicked
12+
- > Let's say you bring your car into the shop, and you need to know when it's ready. The inefficient way: Come back every 15 minutes and ask if it's ready yet. The better way: Leave your phone number and ask that they notify you when it's ready. Now let's say you have an HTML element, and you need to know when it's been clicked. The inefficient way: Come back every 15 ms and ask if there's been a click. The better way (and how event listeners work): Give the HTML element a function and ask that they invoke it for you when a click happens.
13+
- This is really a problem because Javascript is single threaded
14+
- When you block the thread... nothing else can work. Add this to the script and see what happens:
15+
```
16+
for (let i = 0; i < 100000; i++) {
17+
console.log(`Executing for the ${i}th time...`);
18+
}
19+
```
20+
- Even listeners work using callbacks. After a event is triggered, the passed function gets called.
21+
- We are now in the topic of ASYNCRONOUS programming
22+
> One approach to asynchronous programming is to make functions that perform a slow action take an extra argument, a callback function. The action is started, and when it finishes, the callback function is called with the result.
23+
> \- Eloquent Javascript
24+
- Examples
25+
- addEventListener takes 1) the event and 2) a callback function
26+
- setInterval()... how can we do the same color example using setInterval?
27+
- Mouse move example
28+
- Event loop. Most amazing lecture on this is from [Philip Roberts](https://www.youtube.com/watch?v=8aGhZQkoFbQ).
29+
- Browser engine / Node
30+
- Callback hell
31+
- Request library example: https://github.com/request/request (only node!)
32+
33+
## Scope
34+
35+
## Closures

Week3/color-change-example.html

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!-- Taken from https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events#A_simple_example -->
2+
<!DOCTYPE html>
3+
<html>
4+
<head>
5+
<style>
6+
body {
7+
width: 100vw;
8+
height: 100vh;
9+
}
10+
</style>
11+
</head>
12+
<body>
13+
<button id="change-btn">Change Color</button>
14+
<script>
15+
function random(number) {
16+
return Math.floor(Math.random() * (number + 1));
17+
}
18+
function setRandomBackgroundColor() {
19+
const randomColor = `rgb(${random(255)},${random(255)},${random(255)})`;
20+
document.body.style.backgroundColor = randomColor;
21+
}
22+
document.getElementById('change-btn').addEventListener('click', setRandomBackgroundColor);
23+
</script>
24+
</body>
25+
</html>

Week3/mouse-move-example.html

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<style>
5+
body {
6+
width: 100vw;
7+
height: 100vh;
8+
display: flex;
9+
justify-content: center;
10+
align-items: center;
11+
cursor: none;
12+
}
13+
14+
#box {
15+
transition: height 1s, width 1s, background-color 1s;
16+
background-color: lightseagreen;
17+
width: 100px;
18+
height: 100px;
19+
position: absolute;
20+
}
21+
</style>
22+
</head>
23+
<body>
24+
<div id="coordinates"></div>
25+
<div id="box"></div>
26+
<script>
27+
const boxEl = document.getElementById('box');
28+
boxEl.addEventListener('mousedown', () => {
29+
boxEl.style.width = '500px';
30+
boxEl.style.height = '500px';
31+
boxEl.style.backgroundColor = 'coral';
32+
});
33+
34+
boxEl.addEventListener('mouseup', () => {
35+
boxEl.style.width = '100px';
36+
boxEl.style.height = '100px';
37+
boxEl.style.backgroundColor = 'lightseagreen';
38+
});
39+
40+
document.body.addEventListener('mousemove', event => {
41+
const coordEl = document.getElementById('coordinates');
42+
coordEl.innerText = `X: ${event.pageX}, Y: ${event.pageY}`;
43+
44+
boxEl.style.left = `${event.pageX}px`;
45+
boxEl.style.top = `${event.pageY}px`;
46+
});
47+
</script>
48+
</body>
49+
</html>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy