Sebah's Reviews > The JavaScript Pocket Guide

The JavaScript Pocket Guide by Lenny Burdette
Rate this book
Clear rating

by
2711552
's review
Sep 05, 10

bookshelves: summer-10, web-development
Read from September 05 to 06, 2010

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" };

- Access info:
echo member.name; -> Sebah
OR: member["name"]; -> Sebah.

- Change info:
member.name = "Sarah";

- 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);
}


- Delete:
delete member.name;

- Prototypes: (?)
var Jedi = function(name) { this.name = name; };
Jedi.prototype = { theForce: "strong",
lightSaber: function() { console.log("Shrruumm! Shr-zzmm!"); } };

var yoda = new Jedi("Yoda");
yoda.lightSaber(); -> "Shrruumm! Shr-zzmm!"

OR:
var Person = function(name) { this.name = name; };
Person.prototype = {
says : function(message) { console.log(this.name + " says " + message); } };


***

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
likeflag

Sign into Goodreads to see if any of your friends have read The JavaScript Pocket Guide.
sign in »

No comments have been added yet.