\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{camilo.herbert} \pdfinfo{ /Title (es6-cheatsheet.pdf) /Creator (Cheatography) /Author (camilo.herbert) /Subject (ES6 Cheatsheet Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{E6511A} \definecolor{LightBackground}{HTML}{FDF4F0} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{ES6 Cheatsheet Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{camilo.herbert} via \textcolor{DarkBackground}{\uline{cheatography.com/66323/cs/16561/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}camilo.herbert \\ \uline{cheatography.com/camilo-herbert} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 3rd August, 2018.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Constants}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} const EULER = 2.7182818284 \newline \textgreater{} EULER = 13 \newline \textgreater{} EULER \newline \textgreater{} 2.7182818284 // Value stays the same \newline \newline {\bf{Warning! If array or object, the reference is kept}} \newline {\bf{constant. If the constant is a reference to an object,}} \newline {\bf{you can still modify the content, but never change the}} \newline {\bf{variable.}} \newline \textgreater{} const CONSTANTS = {[}{]} \newline \textgreater{} CONSTANTS.push(EULER) \newline \textgreater{} CONSTANTS \newline \textgreater{} {[} 2.7182818284 {]} \newline \textgreater{} CONSTANTS = \{ 'euler': 2.7182818284 \} \newline \textgreater{} CONSTANTS \newline \textgreater{} {[} 2.7182818284 {]}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Object Notation}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{// Computed properties \newline \textgreater{} let key = new Date().getTime() \newline \textgreater{} let obj = \{ {[}key{]}: "value" \} \newline \textgreater{} obj \newline \textgreater{} \{ '1459958882881': 'value' \} \newline // Object literals \newline balloon = \{ color, size \}; \newline // Same as \newline balloon = \{ \newline color: color, \newline size: size \newline \} \newline \newline {\bf{// Better method notations}} \newline obj = \{ \newline foo (a, b) \{ … \}, \newline bar} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Destructuring Arrays}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} let {[}a, b, c, d{]} = {[}1, 2, 3, 4{]}; \newline \textgreater{} console.log(a); \newline \textgreater{} 1 \newline \textgreater{} b \newline \textgreater{} 2} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Destructuring Objects}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} let luke = \{ occupation: 'jedi', \newline father: 'anakin' \} \newline \textgreater{} let \{occupation, father\} = luke \newline \textgreater{} console.log(occupation, father) \newline \textgreater{} jedi anakin} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Spread Operator}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{// Turn arrays into comma separated \newline // values and more \newline \textgreater{} function logger (...args) \{ \newline console.log('\%s arguments', \newline args.length) \newline \seqsplit{args.forEach(console.log)} \newline // arg{[}0{]}, arg{[}1{]}, arg{[}2{]} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{let vs var}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} var average = 5 \newline \textgreater{} var average = (average + 1) / 2 \newline \textgreater{} average \newline \textgreater{} 3 \newline \textgreater{} let value = 'hello world' \newline \textgreater{} let value = 'what is new' \newline // -\textgreater{} throws TypeError: Identifier 'value' has \newline already been declared \newline \newline {\bf{Be aware of Temporal Dead Zones:}} \newline \textgreater{} console.log(val) // -\textgreater{} 'undefined' \newline \textgreater{} var val = 3 \newline \textgreater{} console.log(val) // -\textgreater{} 3 \newline {\bf{Because it's equivalent to:}} \newline \textgreater{} var val \newline \textgreater{} console.log(val) \newline \textgreater{} val = 3 \newline \textgreater{} console.log(val) \newline {\bf{Variables declared with "let/const" do not get hoisted:}} \newline \textgreater{} console.log(val) \newline // -\textgreater{} Throws ReferenceError \newline \textgreater{} let val = 3 \newline \textgreater{} console.log(val) // -\textgreater{} 3} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Promises}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{new Promise((resolve, reject) =\textgreater{} \{ \newline request.get(url, (error, response, \newline body) =\textgreater{} \{ \newline if (body) \{ \newline \seqsplit{resolve(JSON.parse(body));} \newline \} else \{ \newline resolve(\{\}); \newline \} \newline \}) \newline \}).then(() =\textgreater{} \{ ... \newline \}) \newline .catch((err) =\textgreater{} \newline throw err) \newline // Parallelize tasks \newline Promise.all({[} \newline promise1, promise2, promise3 \newline {]}).then(() =\textgreater{} \{ \newline // all tasks are finished \newline \})} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Classes, Inheritance, Setters, Getters}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{class Rectangle extends Shape \{ \newline constructor(id, x, y, w, h) \{ \newline super(id, x, y) \newline this.width = w \newline this.height = h \newline \} \newline // Getter and setter \newline set width(w) \{ \newline this.\_width = w \newline \} \newline get width() \{ \newline return this.\_width \newline \} \newline \} \newline class Circle extends Shape \{ \newline constructor(id, x, y, radius) \{ \newline super(id, x, y) \newline this.radius = radius \newline \} \newline do\_a(x) \{ \newline let a = 12; \newline super.do\_a(x + a); \newline \} \newline static do\_b() \{ ... \newline \} \newline \} \newline Circle.do\_b()} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Binary, Octal and Hex Notation}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} 0b1001011101 // 605 \newline \textgreater{} 0o6745 // 3557 \newline \textgreater{} 0x2f50a // 193802} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Arrow Function}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} setTimeout(() =\textgreater{} \{ \newline … console.log('delayed') \newline … \}, 1000)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Equivalent with Anonymous Function}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} setTimeout(function () \{ \newline … console.log('delayed') \newline … \}.bind(this), 1000)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Equivalent with IIFE}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} (function () \{ \newline … var cue = 'Luke, I am your father' \newline … console.log(cue) // 'Luke, I am – \newline … \}()) \newline \textgreater{} console.log(cue) // Reference Error} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{New Scoped Functions}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} \{ \newline … let cue = 'Luke, I am your father' \newline … console.log(cue) \newline … \} \newline \textgreater{} 'Luke, I am your father'} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{String Interpolation, Thanks to Template Literals}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} const name = 'Tiger' \newline \textgreater{} const age = 13 \newline \textgreater{} console.log(`My cat is named \$\{name\} and is \newline \$\{age\} years old.`) \newline \textgreater{} My cat is named Tiger and is 13 years old. \newline // We can preserve newlines… \newline let text = ( `cat \newline dog \newline nickelodeon` \newline )} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Default Params}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} function howAreYou (answer = 'ok') \{ \newline conso} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}