Skip to content

Instantly share code, notes, and snippets.

@dekom
Created March 16, 2012 01:31
Show Gist options
  • Save dekom/2048062 to your computer and use it in GitHub Desktop.
Save dekom/2048062 to your computer and use it in GitHub Desktop.
[Notes] Eloquent JavaScript

Basic JavaScript: values, variables, and control flow

There are six basic types of values:

  1. numbers: All numbers in JavaScript are 64-bit. 1 sign bit, 11 decimal bits, and 52 whole number bits.
  2. strings: Standard string, either double or single quotes, with backslash () escapes.
  3. booleans: Standard true/false values with &&, ||, ! operators. Extension! || tests from left to right and returns the first true value. && returns the first false value or the right-most value (last expression to be evaluated)
  4. objects: Variables ... zzz (var ... )
  5. functions: alert, show, confirm, prompt, Number, String, Boolean
  6. undefined values: null and undefined, null == undefined /* => true */, but null === undefined; /* => false */ null !== undefined /* => true */

Functions

function name(args ...) {
    ...
}

Empty return statement returns undefined.

Pure functions = functional programming

Impure functions = procedural or Object-oriented

Functions are first-class citizens. They can be passed around and reassigned. Thus, JavaScript functions also exhibit closure. And...one can create lambda functions (anonymous functions).

function alertIsPrint(value) {
    var alert = print; /* assignment */
    alert(value);
}

Of course, there is lexical scoping, where enclosed functions have access to variables and values of its parent function as well as the top-level environment. "Yo dawg, I heard you like functions, so we put a function inside your function..."

Data structures: Objects and Arrays

In JS, objects are meant to hold other values, that is to say that they're more containers than object-oriented objects.

To create object:

var name = {attribute_name: attribute_value, ...}
name.attribute_name /* => attribute_value */
delete name.attribute_name
name.attribute_name /* => undefined */
name.new_attribute_name = new_attribute_value /* Creates new attribute/value pair */

name{"attribute name with spaces"] = "attribute_value"
name["attribute name with spaces"] /* => "attribute_value" */
name["attribute_name"] /* same thing as name.attribute_name */

"string".length == "string"["length"]

To test existence of an attribute, use the in operator:

"attribute_name" in name /* => true */

Common functions, Google them if you need specifics:

var arr = []
arr.push("one")
arr.pop()
arr.join(" ")

"string".split(delimiter)
"string".slice(i, j)

The Date Object

new Date(year, month, day, hour, minute, second, milliseconds) var date = new Date(...) date.getFullYear(), date.getMonth()... date.getTime() date.setMonth()...

Misc

"Whenever a function is called, a special variable named arguments is added to the environment in which the function body runs. This variable refers to an object that resembles an array. It has a property 0 for the first argument, 1 for the second, and so on for every argument the function was given. It also has a length property."

Here are references for JS: JS DOM and MDN Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment