slides.com/jkohlin/js-dom2/
function algorithm(number) { var triple = number * 3; alert( triple ); } // triple and number are undefined out here
algorithm is a global variable
number is a local parameter that doesn't need a declaration
triple is a local (scoped) variable because it is declared inside the function
var twice = more(4) // 8
function more(number) {
var double = number * 2;
return double;
}
var half = less(12) // error less is not a function
var less = function(number) {
var pointFive = number/2;
return pointFive;
}
Why?
var half = less(12) // error less is not a function
var less;
var half = less(12) // error less is not a function
var less = function(number) {
var pointFive = number/2;
return pointFive;
};
var half;
var less;
Because of the two step compiling process, less is not yet a function when it's being called, it's just undefined so far
Step 1
Step 2
Step 3
var twice = more(4) // 8
function more(number) {
var double = number * 2;
return double;
}
var half = less(12) // error less is not a function
var less = function(number) {
var pointFive = number/2;
return pointFive;
}
Together with your neighbour,
Write an algorithm in pseudo code on a piece of paper
Instruct me what to do on the whiteboard.
4 - 6 statements
Give it a name on the back of the paper
Limited set of options (see next slide):
function add(num1, num2) {
return num1 + num2;
}
alert( add(35, 7) );
function calculate(operator, num1, num2) {
if (operator == "+") {
return num1+ num2;
else if (operator == "-") {
return num1 - num2;
}
}
alert( calculate("+", 35, 7) )
function draw(color, shape, angle) {
var pen = new WhiteboardPen()
pen.color = color;
pen.rotation = angle;
pen.draw(pen.color, shape);
}
Together with your neighbour,
Use parameters to decide Shape, Color, Rotation
Max 10 statements
in that order
function calculate(operator, num1, num2) {
if (num1 == undefined || num2 == undefined || operator == undefined) {
return "Could not calculate. Missing operands";
}
if (operator == "+") {
return num1 + num2;
else if (operator == "-") {
return num1 - num2;
}
}
var math = calculate(); // math is now "Could not calculate. Missing operands"
var math = calculate; // math and calculate now contain the same function.
var diff = math("-", 45, 3); // evaluates to 42
var calculator = {
plus: function (a, b) { calculate("+", a, b) },
minus: function (a, b) { calculate("-", a, b) }
}
function draw(color, shape, callback) {
var pen = {}
pen.color = color;
pen.shape = shape;
callback(pen)
}
draw ( "red", "–", function(p){
if (p.color == "red") {
rotate(p.shape, 45deg);
}
} ) ;
1's (write f1 on the back)
2's (write f2 on the back)
3's
Form Events | Description |
---|---|
reset | when the form is reset |
submit | when the form is submitted |
change | when the form element changes |
select | when the element is selected |
blur | when the element loses focus |
focus | when the element gets focus |
input | when something is typed in an input box |
Keyboard Events | Description |
---|---|
keydown | when a key is pressed |
keyup | when a key is released |
keypress | when a key except Shift, ctrl/cmd.. is pressed |
Document Event | Description |
---|---|
DOMContentLoaded | when the HTML document has been completely loaded and parsed |
load | when the DOM is loaded but not parsed |
Mouse Events | Description |
---|---|
mouseenter | when the mouse is moved onto the element |
mouseover | when the mouse is moved onto the element |
mousemove | when the mouse is moved over an element |
mousedown | when the mouse button (ANY button) is pressed on an element. |
mouseup | when the mouse button (ANY button) is released over an element. |
click | when the mouse button (ANY button) has been pressed and released on an element. |
dblclick | when the mouse button is clicked twice on an element. |
contextmenu | when the right button of the mouse is clicked |
wheel | when the mouse wheel is rotated in any direction. |
mouseleave | when the mouse is moved off the element |
mouseout | when the mouse is moved off the element |
select | when text is being selected. |
PUSH
var button = document.querySelector('button');
button.addEventListener('click', callback);
/* a callback is a function that is to be
called when the event happens */
Contains information about what just happened
PUSH
var button = document.querySelector('button');
button.addEventListener('click', logMe);
function logMe(event) {
console.log(event)
}
function addEventListener(eventType, callback) {
//var t = eventType.caller('mumbojumbocode')
var e = { target: t, preventDefault: function(){} };
callback(e);
}
var event = {
clientX: 94,
clientY: 56,
target: "<li>",
timeStamp: 1519786715062,
type: "click",
preventDefault: function() {
//don't behave like you normally do.
}
}
property / method | description |
---|---|
currentTarget | the object the eventListener was added to |
target | the object that dispatched* the event |
timeStamp | when the event happened |
preventDefault | function: stops default behaviour from happening |
*Often the thing you clicked
input types values and submission
<form action="">
<input type="text" value="">
<input type="tel" value="">
<input type="submit" value="Send">
</form>
document.forms[0].addEventListener("submit", holdIt);
/* inside addEventListener, an object called event is available
it contains properties and methods about the actual event */
function holdIt(event) {
event.preventDefault();
// stops the submit event (page reloading)
event.target.style.display = "none";
// target is the element that fired the event
event.target.removeEventListener("submit", holdIt);
// removeEventListener will remove an event listener
alert(document.querySelector("input[type=text]").value);
}