Skip to content

Instantly share code, notes, and snippets.

// The 'click' event which listens for a click.
document.getElementById('btn').addEventListener("click", turnThirdDivBlue);
// the function called as a callback above.
function turnThirdDivBlue() {
document.getElementById("thirdDiv").style.background = 'blue';
document.getElementById('btn').innerHTML = "Turned blue on click!"
}
<!DOCTYPE html>
<html>
<head>
<meta charset = "UTF-8"/>
<link rel="stylesheet" href="style.css">
</head>
<body>
<!-- the three divs -->
<div id="firstDiv" class='divs'></div>
<div id="secondDiv" class='divs'></div>
var http = require('http'); // required for all steps.
// STEP #1 Basic server:
http.createServer(function(request,response) {
response.writeHead(200); // Tell the client if response is good.
response.write('My server worked!'); // the response body
response.end(); // end the connection.
}).listen(3000); // you can change this to another port if you want, but then you would have to visit that port in the browser too.
// STEP #1.5 Basic Server Continued...
// non-blocking / async code:
var fs = require('fs');
console.log('ALERT: Escape from bunny prison!');
// notice the method is called readFile not readFileSync.
fs.readFile('wishlist.html', function(err, wishlist) {
if(err) throw err;
console.log(wishlist.toString());
})
console.log('ALERT: Pounce owner!');
// blocking / sync code:
// you have to require the file system(fs), Node.js's core module for it to work:
var fs = require('fs');
// First read the file 'wishlist.html' synchronously using the readFileSync method:
// We have to use the toString because it returns as a buffer.
// You can learn about buffers in the resources I provide at the end of this article.
var wishlist = fs.readFileSync('wishlist.html').toString();
//print the file
console.log(wishlist);
Things I will pamper Brigadier Fluffykins with:
1. Carrot Bits
2. Lettuce Bits
3. Rasberry Bits
4. Toys
5. Hugs
class Bunny {
constructor(name, age, favoriteFood){
this.name = name;
this.age = age;
this.favoriteFood = favoriteFood;
}
eatFavFood() {
console.log(`"Mmmm! That ${this.favoriteFood} was delicious", said ${this.name} the ${this.age} year old bunny.`);
};
// anonymous:
const Bunny = class {
etc...
};
const BunnyBurgerKins = new Bunny();
// named
const Bunny = class SurferBunny {
whatIsMyName() {
return SurferBunny.name;
function createNewBunny() { new Bunny(); }
createNewBunny(); // ReferenceError
class Bunny {...etc}
createNewBunny(); // Works!
// Normal function declaration
// called before it is declared and it works.
callMe(); // Testing, Testing.
function callMe() {
console.log("Testing, Testing.")
}
// This is called after, as we would do in a function expression,
// and it works too!