Thursday: EL 312 11:45am - 1:25pm
Friday: WVG 102 3:25pm - 5:05pm
(show page/page.html)
JavaScript is:
Examples:
Object prototypes:
function Posn(x, y) {
this.x = x;
this.y = y;
this.dist_from_origin = function() {
return Math.sqrt(this.x * this.x + this.y * this.y);
};
}
var aa = new Point(0, 10);
aa.dist_from_origin();
Problems:
A note on "this":
let default_posn = {
x: 0,
y: 0,
this.dist = function() {
return Math.sqrt(this.x * this.x + this.y * this.y);
};
};
let posn1 = Object.create(default_posn, {x: 5, y: 7});
var body = document.children[0].children[1];
var new_h1 = document.createElement("h1");
new_h1.innerText = "New Heading";
var ch0 = body.children[0];
body.insertBefore(new_h1, ch0);
body.children[0].remove();
body.insertBefore(new_h1, ch0);
new_h1.style.color = "purple";
new_h1.style;
// Align is only available on block elements that can contain text.
new_h1.align = "center";
var para = document.getElementById("hello");
para.innerText = "Here's the 'hello' element.";
var bdivs = document.getElementsByClassName("bbb");
for (var ii = 0; ii < bdivs.length; ++ii) {
var div = bdivs[ii];
div.innerText = "bbb div #" + ii;
}
// Array like objects are weird.
// In modern browsers, we can just copy them to am array.
var bda = Array.from(bdivs);
bda.forEach(function (div) {
div.innerText = "this is a div";
});
var bdivs2 = document.querySelectorAll(".bbb"); // CSS-style selector
var bdiv = document.querySelector(".bbb"); // Gets first matching element
var para2 = document.querySelector("#hello"); // Gets first matching element
Traditional JavaScript doesn't have a module system.
That means any names that are declared at the top level of your code - which is especially common for fuctions - are global.
Luckily, JavaScript provides lexical scoping, which can be used to simulate modules.
(See code.js)
JavaScript also has a great feature: assignment to an unknown variable name declares a new global variable.
This can be disabled with the "use strict" mechanism, which requires that all variables be explicitly declared (with var, let, or const) before use.
Once we start writing modern JS we won't need to worry as much about this stuff - until we try to debug our code, and then we realize that this is what the transpiler does.
<script>
//alert("Scripts execute in order, blocking page rendering.");
</script>
<p><button onclick="alert('clicked button');">Click me</button>
<p><button id="button2">Button 2</button>
<script>
function showPopup() {
alert("clicked button 2");
}
var btn = document.getElementById("button2");
btn.onclick = showPopup;
</script>
<p><a href="http://google.com/" id="link1">Click Me</a>
<script>
function showPopup2(ev) {
//ev.preventDefault();
alert("clicked button 2");
}
var link1 = document.getElementById("link1");
link1.onclick = showPopup2;
</script>
<p><button id="button3">Button 3</button>
<p id="b3text">Button 3 text</p>
<script>
var b3p = document.getElementById("b3text");
function changeTextBack() {
b3p.innerText = "Button 3 text";
}
function changeText() {
b3p.innerText = "some other text";
window.setTimeout(changeTextBack, 2000);
}
var btn3 = document.getElementById("button3");
btn3.addEventListener("click", changeText);
</script>
After we get going, we're going to pull in some libraries to make things more complicated, and possibly easier: