ES6

About

Checklist


Notes

1. Unary + to convert values to numbers:
2. false, 0, empty strings (""), NaN, null, and undefined all become false; everything else is true.
3. let: block level | const: block level
4. var: visible everywhere
5. An important difference between JavaScript and other languages like Java is that in JavaScript, blocks do not have scope; only functions have a scope.
6. `==` operator performs type coercion if you give it different types. To avoid type coercions, use `===`
7.
8.

Objects

Object initialization

Functions (not classes)

Anonymous functions:
Lookup IIFE - Immediately Invoked Function Expression

Custom Objects


function Person(first, last) {
this.first = first;
this.last = last;
this.fullName = function() {
return this.first + ' ' + this.last;
};
this.fullNameReversed = function() {
return this.last + ', ' + this.first;
};
}
var s = new Person('Simon', 'Willison');

Prototype Chain

var s = new Person('abstract', 'xan');
Person.prototype.firstNameCaps = function() {
return this.first.toUpperCase();
};
s.firstNameCaps(); // "ABSTRACT"


Inner functions

Nested functions can share variables in their parent, so you can use that mechanism to couple functions together when it makes sense without polluting your global namespace — "local globals" if you like. -- Inner functions

function parentFunc() {
var a = 1;

function nestedFunc() {
var b = 4; // parentFunc can't use this
return a + b;
}
return nestedFunc(); // 5
}

Closures


function makeAdder(a) {
return function(b) {
return a + b;
};
}
var add5 = makeAdder(5);
var add20 = makeAdder(20);
add5(6); // 11
add20(7); // 27

Scope objects form a chain called the scope chain, similar to the prototype chain.

A closure is the combination of a function and the scope object in which it was created. Closures let you save state — as such, they can often be used in place of objects. A good resource for understanding closures.

Last Updated: 10 April