Provides information on JavaScript, covering such topics as functions, flexible objects, building slideshows and dropdown menus, and creating Web pages with Ajax.
try { // Declaring a variable without an identifier // throws an error var; } catch (exception) { // The exception object has a message // that describes the error console.log(exception); } finally { // This code always runs regardless of // whether an error occurred console.log("Always executes"); } The finally block of this statement is optional, but the catch block is not.
***
Throwing errors to alert the user when something wrong happens. Ex: function argumentRequired(arg) { if (arg == null) { throw new Error("arg cannot be null or undefined.");} }
*** Methods: - typeof (value); -> outputs the type of "value" - replace(pattern, replacement) - "The indexOf() method returns the position of the first occurrence of a substring. If the substring doesn’t exist inside the string, it returns -1." - expression ? ifTrue : ifFalse;
***
"The greater-than (>) and less-than (<) operators compare strings in alphabetical order...sort of. A is less than Z, but uppercase letters are “lower” than lowercase letters, so Z is less than a."
***
Arrays: var myArray = [1, 2, 3, 4, 5]; for (var i = 0, l = myArray.length; i < l; i++) { console.log(myArray[i]); }
***
Functions:
- Setting default value in case no argument was passed. function sayHello(name) { name = name || "Lenny"; console.log("Hi " + name + "!");} sayHello(); -> "Hi Lenny!" sayHello("Sally"); -> "Hi Sally!";
- Passing multiple arguments as objects is safer and easier. drawElement({ color: "red", border: 4, left: 100, top: 10 });
- Cache results in functions that are called mroe than once. var cache = {}; return function(n) { // If we've already solved for this value of n, it will be stored in the cache, so we can return it if (cache[n]) {return cache[n];} And if not, execute code and cache the result before returning it.
- (function(d, c) { d[c] = d[c].replace(/\bno-js\b/, "js"); })(document.documentElement, "className"); "By passing in the repetitive parts of that line of code as the arguments of a self-invoking function, I can reference them as single-character variable names (document.documentElement becomes d, and "className" becomes c)."
***
Objects:
- var member = {}; var member = {name: "Sebah", language: "E", hands: "2" };
- Looping over properties: for (var propertyName in myObject) { // Print out the name of the property console.log("name: " + propertyName); // Print out the property value var value = myObject[propertyName]; console.log("value: " + value); }
HTML -> content CSS -> presentation JavaScript ->behavior
***
Cookies:
- document.cookie = "mbcookies=close"; - // This secure cookie will last a year document.cookie = "name=value;max-age=" + (60*60*24*365) + ";secure;";
-function getCookie(name) { // Get each individual cookie by splitting on semicolons and possible spaces var cookies = document.cookie.split(/;\s*/); // Make a regular expression to match the name and value in a subpattern var pattern = new RegExp("\\b" + name + "=(.*)"); // Check each cookie until you find a match for (var i = 0, l = cookies.length; i < l; i++) { var match = cookies[i].match(pattern); if (match) { return decodeURIComponent(match[1]);}} }
getCookie("mbcookies"); "close"
***
DOM, Nodes (getElement), Events ***twitter like thing -> Ajax, appendto
Good size book to carry. Not enough practical examples and explanations through comments just code snippets. My style of learning doesn't fit this book while others may find it more suitable.