Jump to ratings and reviews
Rate this book

The JavaScript Pocket Guide

Rate this book
Provides information on JavaScript, covering such topics as functions, flexible objects, building slideshows and dropdown menus, and creating Web pages with Ajax.

312 pages, Paperback

First published April 2, 2010

2 people are currently reading
37 people want to read

About the author

Ratings & Reviews

What do you think?
Rate this book

Friends & Following

Create a free account to discover what your friends think of this book!

Community Reviews

5 stars
5 (20%)
4 stars
12 (48%)
3 stars
4 (16%)
2 stars
4 (16%)
1 star
0 (0%)
Displaying 1 - 3 of 3 reviews
Profile Image for Sebah Al-Ali.
477 reviews3 followers
September 6, 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
Profile Image for Dave.
13 reviews
December 1, 2011
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.
Displaying 1 - 3 of 3 reviews

Can't find what you're looking for?

Get help and learn more about the design.