2. “O wonder!
How many goodly creatures are there here! How
beauteous mankind is! O brave new world! That
has such people in't!”
- Shakespeare’s The Tempest
35. // Prevent type coercion
var sum = parseInt("5", 10) + 6 + 7; // 18
36.
37. // Various "false" values
var nullVal = null;
var undefinedVal = undefined;
var zeroVal = 0;
var falseVal = false;
var emptyString = "";
// All would equal false in an if-clause
if (emptyString) {
// Would never go in here
}
41. // Using arguments
function friends (friend1, friend2) {
return friend1 + " & " + friend2;
}
// Lieutenant Dan & Bubba
friends("Lieutenant Dan", "Bubba");
// Lieutenant Dan & undefined
friends("Lieutenant Dan");
42. // Using the arguments collection
function friends () {
var allFriends = [];
for (var i=0, il=arguments.length; i<il; i++) {
allFriends.push(arguments[i]);
};
return allFriends.join(" & ");
}
// Lieutenant Dan & Bubba
friends("Lieutenant Dan", "Bubba");
// Lieutenant Dan
friends("Lieutenant Dan");
43.
44. // Scope - global or local
// Global
var quote = "I had run for 3 years, 2 months,
14 days, and 16 hours."
function () {
// Local
var pantherParty = "I'm sorry I had to
fight in the middle of your Black Panther
party.";
// Global
question = "And so, you just ran?";
}
45. // Global
function meetingJFK () {
var JFKQuestion = "Congratulations, how do
you feel?";
// Local
function forrestReply () {
return "I gotta pee.";
}
return forrestReply();
}
meetingJFK(); // I gotta pee
forrestReply(); // Error: not accessible
64. <video controls>
<source src="swedish-flag.mp4">
<source src="swedish-flag.ogv">
<p>
Sorry, your web browser doesn't
support the video element.
</p>
</video>
89. // Using JSON
var info = {
"language" : "Polish",
"location" : "Warsaw"
};
// Save as string
localStorage.setItem("info", JSON.stringify(info));
// Load as JSON object
alert(JSON.parse(localStorage.info));
92. <!--
The multiple attribute allows for
uploading of multiple files
-->
<input id="files-upload" type="file" multiple>
93. var filesUpload = document.getElementById("files-upload");
filesUpload.onchange = function () {
// Access to data about all files
var files = this.files;
for (var i=0, il=files.length; i<il; i++) {
file.name; // Get the name of the file
file.size; // Get the size of the file, in bytes
file.type; // Get the type of the file
};
};
94.
95. // Supported in Firefox and Google Chrome
if (typeof FileReader !== "undefined") {
var img = document.createElement("img"),
reader = new FileReader();
reader.onload = function (evt) {
img.src = evt.target.result;
};
reader.readAsDataURL(file);
}
103. var ws = new WebSocket("ws://robertnyman.com/wsmagic");
// Send data
ws.send("Some data");
// Close the connection
ws.close();
104. var ws = new WebSocket("ws://robertnyman.com/wsmagic");
// When connection is opened
ws.onopen = function () {
console.log("Connection opened!")
};
// When you receive a message
ws.onmessage = function (evt) {
console.log(evt.data);
};
// When you close the connection
ws.onclose = function () {
console.log("Connection closed");
};
// When an error occurred
ws.onerror = function () {
console.log("An error occurred")
};