Skip to content

Instantly share code, notes, and snippets.

@hhoopes
Last active May 8, 2016 22:42
Show Gist options
  • Save hhoopes/b8d989d2a4ea740056aea2b95bcec770 to your computer and use it in GitHub Desktop.
Save hhoopes/b8d989d2a4ea740056aea2b95bcec770 to your computer and use it in GitHub Desktop.
notes from reading Ch. 3, 15, 16, 17 of Speaking JavaScript

Ch. 3

  • dynamic
  • dynamically typed
  • functional & object=oriented
  • fails silently
  • deployed as source code
  • part of web platform

doesn't have integers, only fpu by specification, but engines use integers

arrays can have holes

elegant:

  • first class functions
  • closures
  • prototypes
  • object literals
  • array literals

last two let you start with objects and introduce abstractions like constructor, later let you implement block scoping, modules, and inheritance APIs

Ch. 15

functions:

  1. call directly say("hello")
  2. Constructor new Hello() (lets you create new objects and starts with uppercase)
  3. method obj.method() (store a method on an object which you cna invoke with that object)

defining functions:

expression - produces a value, a function object

console.log(add(2,3));```

or 
```var addition  = function add(x,y)``` (generally so function can refer to itself)

#### declaration - declares variable, creates a function, and assigns to variable
```function add(x,y){
    return x + y;
};```

#### Constructor - evals JS stored in strings, usually will use expression or declaration instead
```var add = new Function('x', 'y', 'return x + y');

Name your function expressions for debugging

declarations have two advantages over expression : hoisted, so you can call them earlier than they appear, and they have a name

be able to control function calls with call, apply, and bind: they supply a value for `this` so are interesting in OO context

bind: creates a new function that binds one of the variables to a set pattern

apply: useful whenever a function accepts multiple arguments in an array-like manner, but not an array.

#### Extra or missing params
extra: can be retrieved from the `arguments` variable

fewer: missing are `undefined`

I think this is my preferred/favorite way to check optional arguments and set a default:
```optional = optional || 'default value';```

Python-style named parameters (e.g., function (arg1 = 3, arg2 = "b")) provide a description and work well with optional arguments.In JS you can approximate that by passing in an object literal passed in as a single "real" param

# Ch. 16
Shadowing: with identically named variables at global and local scope, the local scope blocks the value of the globally scoped variable, which is only accessible again once the inner scope is over

Ruby is block-scoped, but JS is function-scoped, that is, you have access to a variable within the whole function, not just the same scope. 

Hoisting, as mentioned earlier, moves all var declarations to the beginning for their function, but not the assignments. However, you should still declare variables close to where they're being used. (but hoisting will still occur)

immediately invoked function expression (IIFE, pronounced “iffy”): a simulation of block scoping to avoid conditionally assigned variables being accessible elsewhere in the function

You declare a function immediately like this:
```if (condition) {
    
    (function () {  // open block
            
            var tmp = ...;
            
            ...
        
        }());  // close block```

Can achieve similar effect with `!function` or `void function`

IIFEs also allow attaching private data to a function without declaring a global and keep the function with its state. 

Also allow:
* creating fresh env
* keeping global data private to a constructor
* attaching blobal data to a singleton or method

global objects are named differently in browser vs. in Node.js 

```(function (glob) {
    // glob points to global object
}(typeof window !== 'undefined' ? window : global));```


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