E to the C to the M to the A to the...
if typeOf null blah blah
//install
[4] * [4] // 16
[] * [] // 0
[] * {} // NaN
[4, 4] * [4, 4] // NaN
({} * {}) // NaN
An S? Seriously?
"Ecma International is an industry association founded in 1961, dedicated to the standardization of information and communication systems."
//tinyPiglet.js
export const numEars = 2;
export default rubTummy function(){
return "squeak!";
}
privateMethod = function(){
return "well I never!";
}
//or
export {numEars, rubTummy}
import {rubTummy, numberOfEarsOnAPiglet as numEars} from "tinyPiglet.js";
rubTummy(); //"squeak!"
console.log(numEars); //2
import * as piglets from "tinyPiglet.js"; //everything
piglets.rubTummy() //"squeak"!
import "tinyPiglet.js"; //side-effects only
import tinyPiglet from "tinyPiglet"; //default import
_.sample(['Prototype','Module', 'Revealing Module', 'etc...'])
import { cuteMeatAnimal } from 'cuteMeatAnimal';
class tinyPiglet extends cuteMeatAnimal {
export const numEars = 2;
constructor(){
console.log('Stumble in and roll about on the floor.');
}
export default static rubTummy function(){ //Static: tinyPiglet.rubTummy
return "squeak!";
}
privateMethod = function(){ //instance: new TinyPiglet().privateMethod()
return "well I never!";
}
}
var cat, toys = [], scritchesPerMinute;
let myCatsMood = 'cheerful';
const myCatsDisdainForMe = 'epic';
myCatsDisdainFormMe = 'gone!!' // ERROR!
You maay have seen this before in another language...
function feedCat(
food = 'catfood',
amount = function(){Math.random()},
foodBrand = food + 'Co'
){
if (food === 'human') callPolice();
}
feedCat() //will have defaults!
this
from parent (no self
or .bind()
.map()
, .filter()
, .reduce()
.
//ES5ish
function(number) {
return number % 2;
}
//ES6!!!
number => number % 2
(parameters) => {} // braces will *remove* implicit return
//ES5ish
var example = 'This' + fetchCompliment() + 'string can go \
to another line and keep going'
//ES6!!!
let example = `This ${fetchCompliment()} string can go
to another line and keep going`;
Spread is like a cross section of an array.
let partsToPet = ['back', 'tummy', 'tail']
petTheseParts(...partsToPet);
//petTheseParts('back', 'tummy,', 'tail')
//Trick! quickly convert an object or collection to an array
[...document.querySelectorAll('')].forEach{...}
//or array building:
let foo = [2, 3, 4];
console.log(1, ...foo, 5) // '1 2 3 4 5'
Rest is not sleep ๐. PHP calls this the 'splat' operator๐ฆ.
//a flexible number of parameters
function petTheseParts(...parts){
console.log(parts) //['back', 'tummy', 'tail'];
}
Pull things out of an array
//response.kittens.thisKitten = {name: 'Smuggler'}
let {name, hairColour, favouriteSpot = 'myChair'}
= response.kittens.thisKitten;
name //"Smuggler" vs console.log(response.kittens.thisKitten.name)
hairColour //undefined
favouriteSpot //myChair
You can rename what you extract but it's backwards ๐
let {name: nameOfTheKittenIWillPet} = response.kittens.thisKitten;
name //undefined
theNameOfTheKittenIWillPet //Smuggler
//function parameter defaults when you're CALLING them?
shooCat(favouriteSpot = 'keyboard');
//or this ridiculousness:
var { x: { y: { z: w } } } = o1;
This one is pretty straightforward, but awesome!
function updateKitten(newKitten){
Object.assign({}, defaultKitten, currentKitten, newKitten);
}
let currentKitten = {favouriteToy: 'packing peanuts'}
inspectKitten(currentKitten);
updateKitten(currentKitten, {favoriteToy: 'my soft, mushy fingers'});
{
[surprisePropertyName()]: 'uuuuhh... 7?',
['theHolySwordOf' + myName]: swordStats
}
Object.is({ name }, {name: name}); //true
//install
$ npm install --save-dev babel-loader babel-core
//project config:
module: {
loaders: [
{ test: /\.js$/, exclude: /node_modules/, loader: "babel-loader"}
]
}
//or per file:
var Person = require("babel!./Person.js").default;
$ npm install --save-dev grunt-babel
grunt.initConfig({
"babel": {
options: {
sourceMap: true
},
dist: {
files: {
"dist/app.js": "src/app.js"
}
}
}
});
grunt.registerTask("default", ["babel"]);
And everything in between
And yes, this does all assume you're using NPM for package management.