\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{ott} \pdfinfo{ /Title (javascript-typescript.pdf) /Creator (Cheatography) /Author (ott) /Subject (JavaScript / TypeScript 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}{A3A3A3} \definecolor{LightBackground}{HTML}{F3F3F3} \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{JavaScript / TypeScript Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{ott} via \textcolor{DarkBackground}{\uline{cheatography.com/62692/cs/16054/}}} \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}ott \\ \uline{cheatography.com/ott} \\ \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 10th June, 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{tabularx}{17.67cm}{p{3.6267 cm} x{13.6433 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{宣告 / Declare}} \tn % Row 0 \SetRowColor{LightBackground} var & 常數, 變數, 函 \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} let & 變數, 函式 \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} const & 常數, 函式 \tn % Row Count 3 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.7358 cm} x{5.3984 cm} x{5.7358 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{函式 / Function (copy)}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{函式宣告數(Function} Declaration) & function A(參數) \{ 定義 \} & \seqsplit{不管定義寫在哪,執行時都會被移到最前面,因此到處都可以呼叫,不用管寫程式時的先後順序。} \tn % Row Count 10 (+ 10) % Row 1 \SetRowColor{white} \seqsplit{具名函式表達式(Function} Expression) & let a = function A(參數) \{ 定義 \}; & \seqsplit{須在前面定義,後面才能呼叫。函式名稱只有在此函式的定義內有效,很沒用。} \tn % Row Count 19 (+ 9) % Row 2 \SetRowColor{LightBackground} \seqsplit{匿名函式表達式(Function} Expression) & let a = \seqsplit{function(參數)} \{ 定義 \}; & \seqsplit{須在前面定義,後面才能呼叫。} \tn % Row Count 23 (+ 4) % Row 3 \SetRowColor{white} \seqsplit{立即函式(Immediately-Invoked} Function \seqsplit{Expression/IIFE)} & (function(參數)\{ 定義 \})(參數); & \seqsplit{會立刻執行,適合將} Init \seqsplit{的東西寫在裡面,再放在適當位置。} \tn % Row Count 30 (+ 7) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.7358 cm} x{5.3984 cm} x{5.7358 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{函式 / Function (copy) (cont)}} \tn % Row 4 \SetRowColor{LightBackground} \seqsplit{箭頭函式(Arrow} Function) & let a = (參數) =\textgreater{} \{定義\}; & \seqsplit{函式裡的this,是指向定義時的對象,而不是運行時的對象。也可以說箭頭函式內部,並不會配置一個自己的this。} \tn % Row Count 12 (+ 12) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{9.4985 cm} x{7.7715 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{函式參數 / Function Parameter}} \tn % Row 0 \SetRowColor{LightBackground} 少傳的參數 & 自動填入 undefined \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} 多傳的參數 & 自動忽略 \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} \seqsplit{參數預設值(Default} parameter) & function A(name = 'Mark') \{\} \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} 其餘參數(Rest Parameter) & function A(...args) \{\} \tn % Row Count 7 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{\seqsplit{函式可以當成參數傳入}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{設定函式的 this 對象}} \tn % Row 0 \SetRowColor{LightBackground} 函式.call(對象, 參數1, 參數2, ...) & \seqsplit{適用情境:確定參數數量的時候,立刻執行。} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} 函式.apply(對象, 參數陣列) & \seqsplit{適用情境:不確定參數數量的時候,立刻執行。} \tn % Row Count 7 (+ 4) % Row 2 \SetRowColor{LightBackground} 函式. bind(對象, 參數1, 參數2, ...) & \seqsplit{適用情境:想得到一個設定後函式} \tn % Row Count 10 (+ 3) % Row 3 \SetRowColor{white} 函式. bind(對象, 參數1, 參數2, ...)() & \seqsplit{加上()可以讓回傳的參數立刻執行} \tn % Row Count 13 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{{[}備註{]} \newline call 立刻執行 \newline apply 立刻執行 \newline bind \seqsplit{回傳一個設定後的函式} \newline \newline {[}參考資料{]} \newline 【优雅代码】深入浅出 \seqsplit{妙用Javascript中apply、call、bind} \newline https://www.cnblogs.com/coco1s/p/4833199.html} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{轉型 / Type Casting}} \tn % Row 0 \SetRowColor{LightBackground} 數字.toString() & 數字轉字串 \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} Sring(數字) & 數字轉字串 \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} Number(字串) & 字串轉數字 \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} parseInt(字串) & 字串轉數字 \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} parseFloat(字串) & 字串轉浮點數 \tn % Row Count 5 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{字串}} \tn % Row 0 \SetRowColor{LightBackground} 單引號 & let a = 'Hi'; \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} 雙引號 & let a = "Hi"; \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} 多行字串 & let a = `Hi`; \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} 樣板字串 & let b = 10; let a = `Hi \$\{b\}`; \tn % Row Count 5 (+ 2) % Row 4 \SetRowColor{LightBackground} 字串.length & 取得字串長度 \tn % Row Count 6 (+ 1) % Row 5 \SetRowColor{white} 字串.trim() & 去空白 \tn % Row Count 7 (+ 1) % Row 6 \SetRowColor{LightBackground} \seqsplit{字串.indexOf(搜尋的字串}, 搜尋起始索引) & \seqsplit{搜尋,找不到會回傳-1,找到會回傳第一個符合的位置。(第二個參數非必要)} \tn % Row Count 12 (+ 5) % Row 7 \SetRowColor{white} \seqsplit{字串.slice(起始索引}, 結束索引) & \seqsplit{子字串,索引從0開始,不包含結束索引的字元。} \tn % Row Count 16 (+ 4) % Row 8 \SetRowColor{LightBackground} \seqsplit{字串.replace(原字串}, 新字串) & \seqsplit{取代所有符合的字串} \tn % Row Count 18 (+ 2) % Row 9 \SetRowColor{white} \seqsplit{字串.split(切割詞)} & 分割字串 \tn % Row Count 20 (+ 2) % Row 10 \SetRowColor{LightBackground} \seqsplit{字串.toLowerCase()} & 轉小寫 \tn % Row Count 21 (+ 1) % Row 11 \SetRowColor{white} \seqsplit{字串.toUpperCase()} & 轉大寫 \tn % Row Count 22 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{陣列 / Array}} \tn % Row 0 \SetRowColor{LightBackground} 宣告 & let a = {[}{]}; \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} 遍歷 & \seqsplit{a.forEach(function()} \{ \}); \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \seqsplit{陣列序列化(逗號分隔)} & let c =a.toString(); \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} \seqsplit{合併陣列內元素} & let c = \seqsplit{a.join(合併符號)} \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} 合併多個陣列 & let c = a.concat(b); \tn % Row Count 8 (+ 1) % Row 5 \SetRowColor{white} \seqsplit{插入/刪除元素(return:} 被刪除的值) & let c = \seqsplit{a.splice(起始索引}, \seqsplit{取代數量/可以是0}, 元素/非必要); \tn % Row Count 12 (+ 4) % Row 6 \SetRowColor{LightBackground} 新增元素(頭) & a.unshift(元素); \tn % Row Count 13 (+ 1) % Row 7 \SetRowColor{white} 新增元素(尾) & a.push(元素); \tn % Row Count 14 (+ 1) % Row 8 \SetRowColor{LightBackground} \seqsplit{取得第一個元素(並移除)} & let c = a.shift(); \tn % Row Count 16 (+ 2) % Row 9 \SetRowColor{white} \seqsplit{取得最後一個元素(並移除)} & let c = a.pop(); \tn % Row Count 18 (+ 2) % Row 10 \SetRowColor{LightBackground} 反轉陣列 & let c = a.reverse(); \tn % Row Count 19 (+ 1) % Row 11 \SetRowColor{white} 切割陣列 & let c = a.slice(頭, 尾); \tn % Row Count 21 (+ 2) % Row 12 \SetRowColor{LightBackground} 排序 & let c = a.sort(); \tn % Row Count 22 (+ 1) % Row 13 \SetRowColor{white} \seqsplit{是否包含(true/false)} & let c =a.includes(值); \tn % Row Count 24 (+ 2) % Row 14 \SetRowColor{LightBackground} \seqsplit{至少有一個元素符合(true/false)} & let c =a.some(函數); \tn % Row Count 26 (+ 2) % Row 15 \SetRowColor{white} \seqsplit{所有元素都必須符合(true/false)} & let c =a.every(函數); \tn % Row Count 28 (+ 2) % Row 16 \SetRowColor{LightBackground} \seqsplit{符合的第一個元素(return:} undefied/值) & let c =a.find(函數); \tn % Row Count 31 (+ 3) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{陣列 / Array (cont)}} \tn % Row 17 \SetRowColor{LightBackground} \seqsplit{符合的所有元素(return:} 陣列) & let c =a.filter(函數); \tn % Row Count 2 (+ 2) % Row 18 \SetRowColor{white} 加總 & let c = \seqsplit{a.reduce(初始值/加總值}, 當前值); \tn % Row Count 5 (+ 3) % Row 19 \SetRowColor{LightBackground} \seqsplit{得到處理後的新陣列} & let c = a.map(函數); \tn % Row Count 7 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{\seqsplit{陣列內容不限定單一型態} \newline \newline {[}範例{]} 找出偶數 \newline let a = {[}0,1,2,3,5,6,7,8,9{]}; \newline let c = a.filter(x =\textgreater{} x\%2 == 0);} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \end{document}