Essential part of web platform, but also used in nonbrowser settings (e.g. Node.js)
Quirks
Does not have block scope, therefore a variable within a block (e.g. for loop) IS available from outside
No built-in modules
JS engines have fixes for many quirks
Elegant parts
First-class functions (have state, can be altered, etc)
Closures (variables within { } are available to each other regardless of when called)
Prototypes (e.g. make methods of one function inheritable by another)
Object literals {a: 'apple'}
Array literals [a,b,c,1,2,3]
Chapter 3 (Eloquent): Functions
Variables created inside a function are local to the function, but you can access variables declared outside the function (global vars) from within the function
Functions are the only things that create a new scope, you can have free-standing blocks
function(future) {code} is a function declaration; it does not follow top-to-bottom flow of control
only use function declarations in the outer-most block; don't put one inside an if statement
If you pass too many arguments to a function, the additional arguments are ignored. Too little, and they're assigned 'undefined'
Closure = being able to reference a specific instance of local variables in an enclosing function
Aim for functions with single responsibility
Chapter 5 (Eloquent): Higher Order Functions
classic for loop is complex for (i=0, i<3, i++), a potentially better way is similar to Ruby's each: products.forEach(function(product, index)) {code would go here});
forEach == less code, fewer OBOEs
As in Ruby, map is destructive, forEach is not. map
Higher-order functions are functions that operate on other functions (e.g. taking them as arguments)
allow us to abstract over actions, not just values
JSON
all property names are wrapped in double quotes & only simple data types are allowed
JSON.stringify / JSON.parse
filter builds a new array with some elements filtered out
reduce combines all elements of an array into a single value
Chapter 6: (Eloquent) The Secret Life of Objects
Methods of an object are function values
when a method is called on an object, this refers to the object called
call, apply, bind are ways to write common methods for multiple objects
addToThis.call(objectCalled, argumentOfFunction)
addToThis.apply(obj, arrayOfArguments)
bind is a way to combine an object and a function
A prototype is another object that is used as a fallback source of properties for an object
A constructor is a function that derives from some shared prototype. Distinguish with new
var blackRabbit = new Rabbit("black");
Prototype-less objects var map = Object.create(null);
Getters/setters run when a property is read or written
Inheritance: there are no classes in JS, only objects. You can set an object's prototype to another object in order for it to inherit methods and attributes from the other object.
Chapter 17: Objects and Inheritance (Speaking JavaScript)
key/value pair in an object == property
key always a string, value is any value, including a function
methods are properties whose values are functions
Dot operator and method assignment are same as Ruby
.delete removes a property
Bracket operator lets you compute the key of a property (e.g. obj['some' + 'Property'])
In functions, this is always an implicit parameter whose value is always the global object (window for browsers)
In methods, this refers to the object on which the method has been invoked
Functions are also objects, thus they have methods of their own
Listing properties
Own property (stored directly on object) == Object.getOwnPropertyNames(obj)
All properties == use a loop
Accessors
Getters are called when a property is read
Setters are called when a property is set to a value
Enumerables
properties created by system == nonenumerable
properites create by user == enumerable
3 ways to protect an object
preventing extensions, sealing, freezing
Constructors
similar to classes in Ruby
start with uppercase letters
create an instance with new
look up constructor of an object with: obj.constructor.name
var Car = function(maxSpeed, driver) {
this.maxSpeed = maxSpeed
this.driver = driver
this.drive = function(speed,time){console.log(speed*time);};
}
to create an instance of ^ this constructor: var myCar = new Car(70, "Ninja Man");
to create a shared function amongst all objects, create a prototype on the original function (Car.prototype.nameofprotofunction)
3 ways to keep object data private
constructor environment
properties with marked keys (underscore dot)
properties with reified keys
Descriptors
value, writable, enumerable, configurable
Javascript Garden
square bracket accessor works for property names that'd otherwise lead to a syntax error
monkey patching is a bad idea (modifying Object's prototypes or other built-in prototypes)
use hasOwnProperty in for/in loop to avoid bringing in prototypes higher up the chain
Occurences of this
global scope
test.foo(); (this == test)
new foo(); -- inside of constructor, this refers to the newly created object
In call or apply, the first argument == this
arguments variable holds arguments passed to function (can pass arguments to another function)
no good reason to use for in with an array (use classic for loop)
JS will apply type coercion wherever possible, use strict equal operator to avoid issues
parser will add semicolons if none exist, so make sure you put them where they need to be
Never use a string as a parameter of setTimeout or setInterval
Chapter 3 (Speaking): The Nature of JavaScript
Chapter 3 (Eloquent): Functions
function(future) {code}
is a function declaration; it does not follow top-to-bottom flow of controlChapter 5 (Eloquent): Higher Order Functions
for (i=0, i<3, i++)
, a potentially better way is similar to Ruby's each:products.forEach(function(product, index)) {code would go here});
map
is destructive, forEach is not.map
filter
builds a new array with some elements filtered outreduce
combines all elements of an array into a single valueChapter 6: (Eloquent) The Secret Life of Objects
addToThis.call(objectCalled, argumentOfFunction)
addToThis.apply(obj, arrayOfArguments)
var blackRabbit = new Rabbit("black");
var map = Object.create(null);
Chapter 17: Objects and Inheritance (Speaking JavaScript)
new
obj.constructor.name
Javascript Garden