1<!DOCTYPE html> 2<html> 3<head> 4<meta http-equiv="content-type" content="text/html;charset=UTF-8" /> 5<title>CoffeeScript</title> 6<meta name="viewport" content="width=device-width, initial-scale=1" /> 7<link rel="canonical" href="https://coffeescript.org" /> 8 9<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png"> 10<link rel="icon" type="image/png" href="/favicon-32x32.png" sizes="32x32"> 11<link rel="icon" type="image/png" href="/favicon-16x16.png" sizes="16x16"> 12<link rel="manifest" href="/manifest.json"> 13<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5"> 14<meta name="theme-color" content="#ffffff"> 15 16<style> 17body { 18 font-size: 14px; 19 line-height: 21px; 20 color: #333; 21 background: #f6f6f6 url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAADCAIAAABee8vuAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAB1JREFUeNpi/P79O8PgAyzMzMyD0FlMDIMSAAQYAC22AvZUamhbAAAAAElFTkSuQmCC); 22 font-family: "Helvetica Neue", "Lucida Grande", "Lucida Sans Unicode", Helvetica, Arial, sans-serif !important; 23} 24.container { 25 width: 950px; 26 margin: 0; 27 padding: 80px 0px 50px 50px; 28 clear: both; 29} 30p, li { 31 width: 625px; 32} 33a { 34 color: #191933; 35} 36h1, h2, h3, h4, h5, h6, b.header { 37 color: #000; 38 margin-top: 40px; 39 margin-bottom: 15px; 40 text-shadow: #fff 0 1px 1px; 41} 42h2 { 43 font-size: 18px; 44} 45h3 { 46 font-size: 14px; 47} 48br.clear { 49 height: 0; 50 clear: both; 51} 52ul { 53 padding-left: 20px; 54} 55b.header { 56 display: block; 57} 58li { 59 margin-bottom: 10px; 60} 61table { 62 margin: 16px 0 0 13px; padding: 0; 63} 64 tr, td { 65 margin: 0; padding: 0; 66 } 67 td { 68 padding: 9px 15px 9px 0; 69 vertical-align: top; 70 } 71 th { 72 text-align: left; 73 } 74table.definitions { 75 width: auto; 76 margin: 30px 0; 77 border-left: 5px solid rgba(0,0,0,0.2);; 78} 79 table.definitions td { 80 text-align: center; 81 padding: 5px 20px; 82 } 83blockquote { 84 margin-left: 0; 85 margin-right: 0; 86} 87code, pre, pre > code, textarea { 88 font-family: Monaco, Consolas, "Lucida Console", monospace; 89 font-size: 12px; 90 line-height: 18px; 91 color: #155; 92 white-space: pre-wrap; 93 word-wrap: break-word; 94} 95 p > code, li > code { 96 display: inline-block; 97 background: #fff; 98 border: 1px solid #dedede; 99 padding: 0px 0.2em; 100 } 101 blockquote > pre { 102 margin: 0; 103 border-left: 5px solid rgba(0,0,0,0.2); 104 padding: 3px 0 3px 12px; 105 font-size: 12px; 106 } 107 td code { 108 white-space: nowrap; 109 } 110.timestamp { 111 font-size: 11px; 112 font-weight: normal; 113 text-transform: uppercase; 114} 115.nowrap { 116 white-space: nowrap; 117} 118div.anchor { 119 position: relative; 120 top: -90px; 121 margin: 0 0 -20px; 122} 123div.code { 124 position: relative; 125 background: #fff; 126 border: 1px solid #d8d8d8; 127 -webkit-box-shadow: 0px 0px 4px rgba(0,0,0,0.23); 128 -moz-box-shadow: 0px 0px 4px rgba(0,0,0,0.23); 129 box-shadow: 0px 0px 4px rgba(0,0,0,0.23); 130 zoom: 1; 131} 132 div.code .minibutton { 133 text-transform: none; 134 position: absolute; 135 right: 8px; bottom: 8px; 136 } 137 div.code .load { 138 left: 8px; right: auto; 139 } 140 div.code pre, div.code textarea { 141 float: left; 142 width: 450px; 143 background: #fff; 144 border: 1px dotted #d0d0d0; 145 border-top-width: 0; 146 border-bottom-width: 0; 147 border-right-width: 0; 148 margin: 15px 3px; 149 padding: 0 0 26px 12px; 150 } 151 div.code pre:first-child { 152 border-left: 0; 153 } 154 155#fadeout { 156 z-index: 50; 157 position: fixed; 158 left: 0; top: 0; right: 0; 159 height: 100px; 160 background: -webkit-gradient(linear, left top, left bottom, from(rgba(255, 255, 255, 255)), to(rgba(255, 255, 255, 0))); 161 background: -moz-linear-gradient(top, rgba(255, 255, 255, 255), rgba(255, 255, 255, 0)); 162} 163 164#flybar { 165 position: fixed; 166 z-index: 100; 167 height: 50px; 168 min-width: 490px; 169 left: 40px; right: 40px; top: 25px; 170 background: #eee; 171 background: -webkit-gradient(linear, left top, left bottom, from(#f8f8f8), to(#dadada)); 172 background: -moz-linear-gradient(top, #f8f8f8, #dadada); 173 border: 1px solid #aaa; 174 border-top: 1px solid #bbb; 175 border-bottom: 1px solid #888; 176 -webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px; 177 -webkit-box-shadow: 0 3px 5px rgba(0,0,0,0.1); 178 -moz-box-shadow: 0 3px 5px rgba(0,0,0,0.1); 179 box-shadow: 0 3px 5px rgba(0,0,0,0.1); 180} 181 #logo { 182 display: block; 183 outline: none; 184 float: left; 185 width: 242px; 186 margin-left: 10px; 187 } 188 #logo svg { 189 width: 225px; 190 height: 40px; 191 margin: 5px 0 0 3px; 192 } 193 #logo path { 194 fill: #28334C; 195 } 196 .navigation { 197 height: 50px; 198 font-size: 11px; 199 line-height: 50px; 200 text-transform: uppercase; 201 position: relative; 202 float: left; 203 padding: 0 20px; 204 border: 1px solid #aaa; 205 border-top: 0; border-bottom: 0; border-left-width: 0; 206 cursor: pointer; 207 } 208 .navigation.toc { 209 border-left-width: 1px; 210 } 211 .navigation:hover, 212 .navigation.active { 213 background: #eee; 214 background: -webkit-gradient(linear, left top, left bottom, from(#eee), to(#f8f8f8)); 215 background: -moz-linear-gradient(top, #eee, #f8f8f8); 216 } 217 .navigation.active { 218 height: 51px; 219 color: #000; 220 background: -webkit-gradient(linear, left top, left bottom, from(#e5e5e5), to(#fff)); 221 background: -moz-linear-gradient(top, #e5e5e5, #fff); 222 } 223 .navigation .button { 224 font-weight: bold; 225 } 226 .navigation .button::selection { 227 background: transparent; 228 } 229 .navigation .contents { 230 display: none; 231 position: absolute; 232 background: #fff; 233 opacity: 0.97; 234 top: 51px; left: 0; 235 padding: 5px 0; 236 margin-left: -1px; 237 border: 1px solid #aaa; 238 -webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px; 239 -webkit-box-shadow: 0 3px 5px rgba(0,0,0,0.2); 240 -moz-box-shadow: 0 3px 5px rgba(0,0,0,0.2); 241 box-shadow: 0 3px 5px rgba(0,0,0,0.2); 242 } 243 .navigation .contents a { 244 display: block; 245 width: 290px; 246 text-transform: none; 247 text-decoration: none; 248 height: 12px; 249 line-height: 12px; 250 padding: 4px 10px; 251 border: 1px solid transparent; 252 border-left: 0; border-right: 0; 253 } 254 .navigation .contents a:hover { 255 border-color: #ddd; 256 background: #eee; 257 } 258 .navigation.active .contents { 259 display: block; 260 } 261 .navigation .contents.menu { 262 z-index: 100; 263 border-top: 0; 264 -webkit-border-top-left-radius: 0; -moz-border-radius-topleft: 0; border-top-left-radius: 0; 265 -webkit-border-top-right-radius: 0; -moz-border-radius-topright: 0; border-top-right-radius: 0; 266 } 267 .navigation .contents.repl_wrapper { 268 padding: 0; 269 position: fixed; 270 width: auto; height: auto; 271 left: 40px; top: 90px; right: 40px; bottom: 30px; 272 background: -webkit-gradient(linear, left top, left bottom, from(#fafafa), to(#eaeaea)); 273 } 274 .navigation .repl_bridge { 275 position: absolute; 276 height: 12px; 277 left: -1px; right: -1px; 278 bottom: -14px; 279 border: 1px solid #aaa; 280 z-index: 5; 281 background: #fff; 282 display: none; 283 border-top-color: #fff; border-bottom-color: #fff; 284 } 285 .navigation.active .repl_bridge { 286 display: block; 287 } 288 .navigation .code .minibutton { 289 top: 10px; right: 10px; 290 width: 40px; 291 text-transform: none; 292 } 293 .navigation .code a.minibutton.permalink { 294 top: 38px; 295 display: block; 296 } 297 298.bookmark { 299 display: block; 300 width: 0; height: 0; 301 position: relative; 302 top: -90px; 303} 304 305.navigation .contents.repl_wrapper .code { 306 cursor: text; 307 -webkit-box-shadow: none; -moz-box-shadow: none; box-shadow: none; 308 background: #181a3a url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAOCAYAAAAmL5yKAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAACZJREFUeNpi/P//PyMDBYCJgULAAsQD64JRL1ApDP6PeoEyABBgAKOyBSJI2bJcAAAAAElFTkSuQmCC); 309 border: 2px solid #555; 310 padding: 0; 311 position: absolute; 312 top: 15px; left: 15px; right: 15px; bottom: 15px; 313} 314 .repl_wrapper .screenshadow { 315 position: absolute; 316 width: 200px; height: 150px; 317 background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAAEsCAYAAADtt+XCAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAEQZJREFUeNrs3Qly27i6gFGKouwF3DV09r+erOTFmp7oBtMIo5HiAIDnVKXct+t2LGvApx8k5c0///zzv4p3Hds/m83m++v5wl2Sv58/f361X3/8+PHh3sjf5fW5uXzZXl6e2/Zr+MMbGnfBeNHQjeyce19v/btDu/5061C8Jl35d6T6YP/7Am0fy4OYCMgSTiEaB9HIJgrPRuLR4373ze0TcRGb9GPShJDU7iEBGXNBOoYn2kE0kng8zlf++dr/XnKSuRebzY3/vRGY5WMSQtLFxOMhIIPE0TiFJ5p7ZblAvDo15PDzPTPNCMz8Mdm3fy6v/boXEwTk4VaFLaplI3F239wN5UZcZotJux58XdaDNii2uATkuWlDNEYPhUjMM8FcC4qwjDOVdFtcphIB+f1CPISzqBzbEIqS4yIs00wlTTiLq1nrfbnGgJyiaeMoGm/HQijyD4uoDJtKumMl22gqWdX21poCYptKLHgcFlF5PSbdNWH7tW1vrSEgwiEWiMocIem2tw5rCUnJATlUrt14tFCcosXi5C7hhajUUVxqQbkakngiKXKtLfGHEg7TBdM79aZ8U8rfIfk+TtKbSIpac0v6YY7Rg7X2cAgGS08pgnI7JLuqkK2tEgLSHePYrzwcJ8Ego6Cs7mK8Xkh2VQHHSHIOyCl6MNZ4cLx7gZ4Eg4yDUq9tOgkh+epNJFkGtcn0ibgPE8fawmHKoMQ3QKucTnpnbe1CSLIKaW4B6SaOw0rCYcrAdFL4dBJC8qv9VIxoIhGQER3DRwfsVxAO0UBQ/p5Oio9J+8Y4+iDXLA60px6QP45zFP6C6W9PAbe3uoq89iS8Qd73ppFkt/RSDk
317hb46+CP69KNGBYTKob00lJIemOj7Tr30eqa3WKN+qP03ILfjclGjBuTIrb5mrfQF/Wwv9L9bTf1ALSnV1V2tRxEg2Y5Y1ZPJUUcTZXdNrvMTpbS0B6U0cXj5Ke1KfoKzBvTOqqoC2uMI0cw6f/JnGQPYWAlDR12KKCtCb/qipoiys6yH5KYRppFn5w94Uc6zBtgKlk7mnkFA62L3am1lIB6Q6S535BoGMbkP9UkuWxkujYyCk6yF58QPbh9Nxc36nH72ZMG1DGVJLt9la4APEUgjLrltacATlF8cj1CWebCsqcSrLe3mrfkIfTfWfd0porIMcQjhy3rOJpwzYVlB2S6spUkktE2i/dllZ78eHkZ2nNEZDfV5QLB5DJa/+YcUjaNfc8xxXsUwfkK8TjnNmTRziAbEMSXcHeRuQjt4Cco3gIByAk80fkfFmDf4U38B9T3OYpAvL9IWBVPh+9LhxAkSHpjouE2/5RjXxwfewj9d8Hy9t4ZPKEOIUnxFE8gBdD0sUkB/uwNo96LHrMCeT3mVaZhMN1HMAY60hdZXBB4mVt3kcH10c5Q2usgBzDXlvqZ1rZrgKmCEl3YWLS21rRGVqfY0RkjGLmEg/bVcCUb06z2NYKZ2j9qkbYzno3IIcM4tE+sIfwx5YVMPUb1W69SfaNahSRtw45vBOQYwYXCJ6EA7D23IzIWwfWhwYk9W2rcw7vAoCiJb8OvbudNSQgqU8epg7AmjTDJPJqQE6Jn6pr6gBSn0ZSjMghROSlyNUv3gGpxuNs6gAymkaSe5Mb1vavV27bK9eBpHqFeXfanKkDyCUi3TUj28RuW7vGt9exfI45gXyF8SbFqcN1HUBuuutGkptGwlr/1Hr/TEAOCX6qrgPlQCnTSFJrWbvWh4g8PFzxKCDHxH6fR7LVBnhjXUtqNyV8FPzDM7PuBaT9C/YJna4bfxQJQGmS+iiUcHrv/l7U7h1EbyePfUJ3rAPlQOniD2Zc/AB7aMDNg+q3JpBDKI/RDmD+dS+ZrfrQgsOzATklctzjVOX1C1sAilsDo+Mhp2cCksJxj9x+2xfAlBFZdE0OTdg/CkgKW1eOdwBE63cUksVc28qKA7L01pXjHQC318dFj4tc28qKA7Lk1tW5smUF8MhpyTfZ/a2sLiDt+b5LfUiieABkEpHQimMckHb6OC14R4gHQAZrZ2jFvgvIYaHp4xT9ASCTNTQ049CEjys5r+UHBygsIp16rm8aDqjvmwV+QZR4AOQfkUOzwA8qHgAFRGTOgJzFA2DyiGzCn8nNVSqn6gLME5HZTvGdIyDiAVBgROYIiHgALBORrAMiHgCFRmTKgPj1swDLmnQdniogzrYCSGcSmWQ9niIg3em6PpIdYHmTrclTBMRxD4D0ppDRt7LGDoh4AKwkIvXIN048ANKOyGjr9FgBcdwDIH2jrtVjBcT0AbCyKaQe6ca43gMgH6Mcr343IGeTB0C2k8hbW1n1CDdAQADyDMhb63e95DcHIN+IDA2Is64A8vfWWj40IKYPgJVPIUMC4sA5QHkReXkKGRKQ2X5dIgCz6H5z7KQBcdwDoNyIvLS7JCAATB4QB84ByvbSOv9qQAAoPyKjBsT0AWAKGRwQAEwhLwXE9AFgChkcEABMIS8FxGm7AOv08LTe+om/QEAA1hmQ89CA+MwrgHW7uwtVD/0PAVjFFHIaOoEAICIvBcSpuwDc7UFt+gBgyBRS3/g/mj4AiKeQ87MBMYEAcLcL9Y3SAMDdNtSmDwCGTCECAsAoAbF9BcAtp0cTCADcmkKuBsRHlwDwKCCnawERDwCenkIEBIC3AuLsKwCeDchZQAAYJSAA8GxEBAQAAQFg5oA4/gHAqwE51+IBwJCICAgAAgLAvAEBgJeZQAAYNIFszmf9AGDYBAIAAgKAgAAgIAAICAAICAACAoCAACAgAAgIAAgIAAICgIAAICAACAgACAgAAgKAgAAgIAAICAAICAACAoCAACAgAAgIAAgIAAICgIAAICAACAgACAgAAgKAgAAgIAAICAAICAACAoCAACAgAAiIuwAAAQFAQAAQEAAEBAAEBAABAUBAABAQAAQEAAQEAAEBQEAAEBAABAQABAQAAQFAQAAQEAAEBAAEBAABASAJzY8fPzaXr2d3Bfzp58+f318vrxF3Bvxt004gG/cDAEMCAgAvM4EAMHgCERAABASAeQMiIgA8HY8qOoguIAC8EpBKQAAQEACWCYiIAPBMPAQEgPcDUgkIAE8GpBIQAEYJiIsKAXgUj/paQEwhADw1fVwLiE/nBeCW+l4wnI0FwK3pYyMgAIwekFv/DoB1q5+JhSkEgIdduBUQUwgAcSueCkhlAgHgURPqO7UxhQBwswf1q8UBwPTxKCA+2gRAPOohAXEwHWDd6qETSBcRUwjAOqePzaO6DK4PAEUHpH4nIM/+fwAoS/32/6FySi/AGuMxSkBMIQCmj7cCIiIApo9Bk4WAAJg+BgdERABMH4OmCqf1ApTp5YvHBQSAWQLS2ooIQHHx2L76H9UDv5FjIQDlGLS7VL/xzUQEoIx41EP/w6HjjuMhAHl7ay1/Z4owhQCsdPp4NyAiArDSeIwREAfUAfINyObdv2CMG7H1WABkYzvG+j/W9GArCyCfyaMe6y8ag7OyANI36lo95tRgCgFYyfQxdkBa20pEAFKNx3bsv3BsIgJQeDymCojjIQDpmGxNnmpScDwEIJ3po57qL57KtnJ9CMCSJl2H6xluvEkEYJnJYzv1N5ijgCICUFA85grIRkQAZo/HZo5vNAcRASgoHnMGpIuIs7MAplvPZ72EolngB+ycPN4Ao8Zj1jfo9WazadbwgwKIx3jadtTn83l3+YfNGn5gAPEYJR6bth3tN20u/9Cs5Qc
317HEI/3hGY03Tdup5B6oTvA2VkAmaydoRW7Kvrm24WmkO/bIyIAL8djkQ+sDa3YVr1Fu51ClvrsKhEBSDweoRG7+Mb8/udLWT4WOKAeR6RZ8s4BSFT3JrtZMB7tgfOPuBv9d/ztAfXdwndUN4mICMB/F2Ev+unmoQ1NfxzqW3IrK46ILS1g7eoqgV+N0d+6uheQpbey+neciABrjseia+C1rat7AWmlsJXVjW6OiwBrsvjxjti1ratHAWm1U8gukTvUlhawpqkjid/mGhrwce/G3vxvw8ecbN2xAOt6o9yu/WH62AwJyPcPlMjxkCRHO4CR1rWktuqj4x7bR+/qH2lCRFIb85rKlhaQt+TWsnatD/Fonrnxz/gIf6FqA4yzfiW5mxLW+qfW+1c+/6r9C8+XP/vEHohuz/BY+SVVQB5TR6pvfHfPxuOVCaQr5scCv4DqlWnElhaQejySPIYb1vaPV27bqwtud5Fhk+iD01QOsAPpvslNcu1s1/RbFwuOGZBWd2ZWqqfTOsAOWJOej8f2mTOuxgpIF5HPhCOyMY0A1qGn4vFZDby+7p0ipj6JmEYAa88Ek0fn3f24dt+svebk1+XrMfF3AafoD8BU4Vjk95TPOXmMMYHEk8hn4pNI97N2HxVgWwsY+41qFp/ZN1Y8qhF/0C4ijQcZWOHUkcWb03C21SjxaI254P/+3KzL130GD3j7x7YW8O46ksWb0faTdcOHI462WzT2xNBGpCvwPpMnwCb8aSNy9poAHq3FUThy2Q7fDbnOY+6AdIvy57/B23xdbnQOT4ZtbyIREiD7cEQfjPgxxW1uJryjP8MZWm1Ezpk8OYQEKGHiiD+SfbIPwp36oHf7Kb51iMgxoyeLkABZhiPEY/vsR7KnHJDv7xFNIofMnjxCAsKR143/73OtJr+0Yq7TbrvzjutMjovcC8m5ctYWlKiO4pFjOL6Pd4QzrWY5M6yZ+cH5jLa0cluEu5Cce1MJkH844jMy8xuZNps6TB27Ob/vEhf+7cKW1j6zLa04JJvq760t21uQ5+s464uKw5bVbon1fKkrx9sf+PuBCyHJdfHtnnznyvYW5PKazXabqheO9iyr3ZxbVqkEpHsguy2tfUZnad16N9Pf3jKVQHrTRrbbVL14bEM4dkvejhQ+u2oXppF9CEkpT1RTCZg2xg5HFU0di3+AbSoffvj9QWSXO2VbwDTSn0qqyrESWOJNXFEfmJrK1JFiQPrTyCHzYyPX3gnVUUDEBKaJRhFbVL1wdMc6mhSmjpQD0p9GvgqZRq490fshERN4/bVU1BbVjalj8ivKSwrI79sWHRs5ZHjdyDMvgO7dhJiAaMThaK/raJY8wyr3gFTVf2dqNe2WVhuTcna17sbENhf8PbUXtz11JRztl2QOkucekM73llb7NUwjh5W8YMQE0VjJr6AOFwS2a/Iul9vcZHYfdweSulN+Tyt6IVXV39tcgkJJz/P+830dP/y/21Xd1JFVLJtMn2wfUUgOKwhJJ35hmU4wZeQfjuSPc5QWkHgx/QwhKe20X9MJpoxyw5HsablrCkinO+03nkjWuHhem04EhVSCscop40o4mlwOkK8lIP2QHMJEstaQVFderIKCYCwfjqawNbesHyb6mRohuRuULirxb1n0eV0MmXiLvyZDONYVkH5IjlFILJJ/RmVrSsF0MXo46igc25J/1mYFj2d8jERIXp9SREUsxEI4VhuQfkh2UUiOnvai4vEVizfDsY3Csaqzy5oVPt7tA/wRheR7i8txklGjUglLso+ZWIwTjfa+a6PR7W6s8r5sVv6i6q7+dJxk/KgIi1CUGI7VbVMJyGPx9taxDUn71VQyeVgqcRklEpVQTD5tdNtU22rFF0EKyH3fv/jJVLJYWB7FpVpBYDZXvoqEaUNAMp1KPnox8Q55ubg8mlhymGD6P59AJDZtdNdthGnD4yEgb7/guydUO4nY4ko/MNWDyWXsaaY/NdyaJCoLUprRqGxRCcgM+ltcx3AWl5ikFZhnF+pHcTlFj/szkSC/aPz+XUPuFQGZU7fFVYlJsbH58joRDW77fwEGANUZsxMt/7FyAAAAAElFTkSuQmCC); 318 } 319 .repl_wrapper .screenshadow.tl { 320 top: 0; left: 0; 321 background-position: 0 0; 322 } 323 .repl_wrapper .screenshadow.tr { 324 top: 0; right: 0; 325 background-position: -200px 0; 326 } 327 .repl_wrapper .screenshadow.bl { 328 bottom: 0; left: 0; 329 background-position: 0 -150px; 330 } 331 .repl_wrapper .screenshadow.br { 332 bottom: 0; right: 0; 333 background-position: -200px -150px; 334 } 335 336#repl_source, #repl_results { 337 background: transparent; 338 outline: none; 339 margin: 5px 0 20px; 340 color: #def; 341 -webkit-tab-size: 2; 342 -moz-tab-size: 2; 343 -o-tab-size: 2; 344 tab-size: 2; 345} 346 #repl_results, #repl_source_wrap { 347 width: auto; height: auto; 348 position: absolute; 349 margin-bottom: 0; 350 top: 10px; left: 10px; right: 10px; bottom: 15px; 351 } 352 #repl_results.error { 353 color: red 354 } 355 #repl_source_wrap { 356 margin-left: 5px; 357 width: 47%; right: 50%; 358 float: left; 359 } 360 #repl_source { 361 padding-left: 5px; 362 width: 100%; 363 height: 100%; 364 border: 0; 365 overflow-y: auto; 366 resize: none; 367 } 368 #repl_results_wrap { 369 white-space: pre; 370 } 371 #repl_results { 372 text-transform: none; 373 overflow-y: auto; 374 left: 50%; 375 border-left-color: #555; 376 } 377 378/*----------------------------- Mini Buttons ---------------------------------*/ 379.minibutton { 380 cursor: pointer; 381 color: #333; 382 text-shadow: #eee 0 1px 1px; 383 font-weight: bold; 384 font-size: 11px; 385 line-height: 11px; 386 padding: 5px 10px 6px; 387 height: 11px; 388 text-align: center; 389 -webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px; 390 box-shadow: 0 1px 2px rgba(0,0,0,0.2); -webkit-box-shadow: 0 1px 2px rgba(0,0,0,0.2); -moz-box-shadow: 0 1px 2px rgba(0,0,0,0.2); 391 border: 1px solid #b2b2b2; border-top-color: #c9c9c9; border-bottom-color: #9a9a9a; 392 background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAAWCAIAAACOpGH9AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAACxJREFUeNpifvr0KRMDAwPT////4RjE//fvHxiD+Mjsv3//4uSD2FD1AAEGAHv9O3d7p0bEAAAAAElFTkSuQmCC) repeat-x left top; 393} 394 .minibutton:active { 395 border-color: #aaa; 396 box-shadow: 0 1px 2px #e4e4e4; -webkit-box-shadow: 0 1px 2px #e4e4e4; -moz-box-shadow: 0 1px 2px #e4e4e4; 397 } 398 .minibutton::selection { 399 background: transparent; 400 } 401 .minibutton ::-moz-selection { 402 background: transparent; 403 } 404 .minibutton.ok { 405 color: #fff; 406 background-image: url(data:image/gif;base64,R0lGODlhAQAYALMAADOtbj3BeyqVXDq6dTClZy2gYzKoazu+eDm1cyyaYDaycj7DfAAAAAAAAAAAAAAAACH5BAAAAAAALAAAAAABABgAAAQMcMlJZzgDKWBISUIEADs=); 407 border-color: #4ba47c; border-top-color: #53b388; border-bottom-color: #459671; 408 text-shadow: #aaa 0 -1px 0; 409 } 410 .minibutton.dark { 411 border: 0; 412 color: #fff; 413 box-shadow: none; -webkit-box-shadow: none; -moz-box-shadow: none; 414 background-image: url(data:image/gif;base64,R0lGODlhAQAYAMQAAFZWVn19fW1tbXd3d3FxcWtra1tbW3V1dWVlZWJiYlJSUnx8fF9fX3l5eWlpaXNzc35+fgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAAAAAAALAAAAAABABgAAAURICSOYrA0w/EQQuEgCWMASggAOw==); 415 text-shadow: none; 416 } 417 .minibutton.error {
418 opacity: 0.5; 419 color: #600; 420 cursor: not-allowed; 421 } 422 423@media (max-width: 820px) { 424 .container { 425 width: auto; 426 padding: 1em; 427 } 428 p, li, table { 429 width: auto; 430 } 431 #fadeout { 432 display: none; 433 } 434 #flybar { 435 position: static; 436 height: auto; 437 min-width: 245px; 438 } 439 #logo { 440 float: none; 441 } 442 .navigation { 443 float: none; 444 border: none; 445 } 446 div.code pre, div.code textarea { 447 border-left: none; 448 border-top-width: 1px; 449 width: auto; 450 float: none; 451 margin: 5px; 452 padding: 10px 5px; 453 } 454 div.code pre:first-child { 455 border-top: none; 456 } 457} 458 459/* Highlight.js syntax highlighting */ 460/* http://jmblog.github.com/color-themes-for-google-code-highlightjs */ 461/* Forked from http://softwaremaniacs.org/media/soft/highlight/styles/tomorrow.css */ 462.tomorrow-comment, pre .comment, pre .title { 463 color: #8e908c; 464} 465 466.tomorrow-red, pre .variable, pre .tag, pre .regexp, pre .ruby .constant, pre .xml .tag .title, pre .xml .pi, pre .xml .doctype, pre .html .doctype, pre .css .id, pre .css .class, pre .css .pseudo { 467 color: #c82829; 468} 469 470.tomorrow-orange, pre .number, pre .preprocessor, pre .built_in, pre .params, pre .constant { 471 color: #000000; 472} 473 474.tomorrow-yellow, pre .class, pre .ruby .class .title, pre .css .rules .attribute { 475 color: #eab700; 476} 477 478.tomorrow-green, pre .string, pre .value, pre .inheritance, pre .header, pre .ruby .symbol, pre .xml .cdata { 479 color: #718c00; 480} 481 482.tomorrow-aqua, pre .css .hexcolor { 483 color: #3e999f; 484} 485 486.tomorrow-blue, pre .function, pre .function .title, pre .python .decorator, pre .python .title, pre .ruby .function .title, pre .ruby .title .keyword, pre .perl .sub, pre .javascript .title, pre .coffeescript .title { 487 color: #21439C; 488} 489 490.tomorrow-purple, pre .keyword, pre .reserved, pre .javascript .function { 491 color: #FF5600; 492} 493 494pre .subst { 495 color: #A535AE; 496} 497 498pre .literal { 499 color: #A535AE; 500} 501 502pre .property { 503 color: #A535AE; 504} 505 506pre .class .title { 507 color: #21439C; 508} 509 510pre .coffeescript .javascript, 511pre .javascript .xml, 512pre .tex .formula, 513pre .xml .javascript, 514pre .xml .vbscript, 515pre .xml .css, 516pre .xml .cdata { 517 opacity: 0.5; 518} 519 520</style> 521 522</head> 523<body> 524 525<div id="fadeout"></div> 526 527<div id="flybar"> 528 <a id="logo" href="#top"><svg xmlns="http://www.w3.org/2000/svg" viewBox="-22 347 566 100"> 529 <title> 530 CoffeeScript Logo 531 </title> 532 <path d="M21.7 351.1c.1.6-.2 1.1-1.2 1.6-1.3-.7-4.1-1.1-6.4-.9-2.5.2-4.6 1-4.3 2.7.4 1.7 2.8 2.7 7.1 2.3 10.5-.9 10.4-8 25.8-9.4 12-1.1 18.7 2.6 19.6 7.1.7 3.5-2.2 6.9-10.9 7.6-7.7.7-12.2-1.4-12.6-3.5-.2-1.1.4-2.7 4.1-3.1.4 1.7 2.5 3.5 7.5 3 3.6-.3 6.6-1.6 6.2-3.6-.4-2.1-4.2-3.3-10.2-2.8-12.2 1.1-15.2 7.8-25.6 8.7-7.4.7-13.4-2-14.2-6-.3-1.5-.3-5 7.5-5.7 4-.3 7.2.4 7.6 2zm-39 41.8c-3.4 4.3-4.9 9.3-4.6 14.2.3 4.9 2.7 8.9 6.5 12 4 3.1 8.3 4 13.2 3.1 1.9-.3 4-1.3 5.9-1.9-4 0-7.4-1.3-10.8-4-3.7-2.7-6.2-6.5-6.8-11.1-.9-4.3 0-8.3 2.4-11.8 2.7-3.4 6.2-5.3 10.8-5.9 4.6-.3 8.6.9 12.6 3.7-.9-1.3-2.2-2.2-3.4-3.4-4-2.7-8.3-4-13.6-2.7-4.8 1-8.8 3.5-12.2 7.8zm53.6-23.1c-12.9 0-24.4-1.3-32.7-3.1-8.9-2.2-13.6-4.6-13.6-7.7 0-1.3.6-2.4 2.4-3.7-5.6 2.2-8.6 4-8.6 6.8.3 3.1 5.3 6.2 15.5 8.6 9.6 2.4 21.9 3.7 36.7 3.7 15.1 0 27.1-1.3 36.7-3.7 10.2-2.4 15.1-5.6 15.1-8.6 0-2.2-2.2-4.3-6.2-5.9.9.6 1.6 1.6 1.6 2.7 0 3.1-4.6 5.6-13.9 7.7-8.6 1.9-19.6 3.2-33 3.2zm36.8 8.6c-9.6 2.2-21.9 3.7-36.7 3.7-15.1 0-27.4-1.6-37-3.7-8.6-2.2-13.2-4.6-14.8-7.1 1.6 10.8 5.3 21 10.2 30 3.7 5.6 7.4 10.5 11.1 15.8 1.6 3.1 2.7 6.2 3.4 9.3 2.4 3.4 5.9 5.6 10.2 6.8 5.3 1.9 10.8 2.7 16.4 2.4h.6c5.6.3 11.5-.6 16.9-2.4 4-1.3 7.4-3.4 9.9-6.8h.3c.6-3.1 1.6-6.2 3.1-9.3 3.7-5.3 7.4-10.2 11.1-15.8 4.9-8.9 8.3-19.1 10.2-30-2 2.8-6.6 5.2-14.9 7.1zm106.2 30.1c-4.8 12.1-17.6 16.9-25.9 16.9-13.4 0-19.9-6-19.9-22.3 0-16.5 7.9-47.3 31.7-47.3 8.5 0 15.2 3.3 15.2 12.1 0 4.8-1.8 8.3-6.4 8.3-1.5 0-3.4-.4-5.2-2.4 2.2-1.1 4.2-4.9 4.2-8.3 0-2.9-1.5-5.6-5.6-5.6-10 0-18.9 23.9-18.9 42.4 0 8.3 2.2 14.2 10.9 14.2 7.1 0 13.5-3.4 17.7-9.1l2.2 1.1zm32.9-16.3c.4.2.7.2 1 .2 4.2 0 10.1-2.7 14-5.5l.8 2.4c-3.4 3.7-9.5 6.5-16.1 7.5-1.5 16.8-10.6 27.3-21.7 27.3-8.4 0-14.5-4-14.5-14.4 0-10.5 6.2-32.2 24.9-32.2 7.8.3 11.6 5.3 11.6 14.7zm-7.7 5c-1.9-.5-2.4-2-2.4-3.8 0-2.5 1.2-4.2 2.8-4.9-.2-3.8-1.1-5.3-3.4-5.3-6.5 0-12 16.6-12 25.6 0 6 1.2 7.3 4.6 7.3 4.2.1 8.9-8 10.4-18.9zm-6.6 39.7c0-8.3 7.1-11 15.8-13.6l10.9-51.9c2.7-13 10.6-15.5 16.5-15.5 4.1 0 8 2.2 9.7 5.7 3.6-4.6 8.4-5.7 12.4-5.7 5.6 0 10.8 3.9 10.8 9.8 0 1.5-.1 2.6-.3 3.7h-4.3c.1-.9.2-1.7.2-2.4 0-2.1-1.7-3.1-3.4-3.1-2 0-4.8 1.1-6.2 7.1l-1.7 7.4h9.1l-.8 3.6h-9l-10.3 49.1c-2.7 13-10.6 15.5-16.5 15.5-5.2 0-8.3-2.3-9.8-5.7-3.5 4.6-8.3 5.7-12.3 5.7-5.6.1-10.8-3.8-10.8-9.7zm9.1 1.8c1.9 0 4.2-1.8 5.4-7.1l1.1-5.3c-5.7 2-10.1 4.4-10.1 9.4
5320 1.2 1.7 3 3.6 3zm21.7 0c1.9 0 4.2-1.8 5.4-7.1l2.2-10.4-9.4 1.8-1.8 8.3c-.5 2.1-1.1 4-1.8 5.6.9 1.3 3 1.8 5.4 1.8zm-1.4-18l9.4-1.7 7.7-36.8h-9l-8.1 38.5zm16.6-56.7c-2 0-4.8 1.1-6.2 7.1l-1.7 7.4h9l2.1-9.5c.2-.7.2-1.3.2-2 .1-2-1.5-3-3.4-3zm37.9 53c7.1 0 11.6-4 16.1-9.2h3.1c-5.2 8.3-12.9 16.8-25 16.8-8.5 0-14.2-4.2-14.2-14.5 0-10.5 5.9-32.3 24.6-32.3 8.1 0 10 4.2 10 8.7 0 10.5-10 18.5-20.9 19.2-.1 1.3-.2 2.5-.2 3.6 0 6.2 2.2 7.7 6.5 7.7zm5.3-34.4c-4.6 0-9.1 9.7-10.9 18.7 7-.5 13.2-7.4 13.2-15 0-2.2-.5-3.7-2.3-3.7zm28.6 33.4c3.4 0 7.8-2.3 10.8-4.8-2 10.4-8.4 13.4-15.8 13.4-8.4 0-14.1-4.2-14.1-14.5 0-10.5 5.9-32.3 24.6-32.3 8.1 0 10 4.2 10 8.7 0 10.6-10 18.5-20.9 19.2-.1.9-.2 2-.2 2.7 0 5.7 2.5 7.6 5.6 7.6zm6.2-33.4c-4.5 0-9.1 10.1-11 18.7 7.1-.4 13.3-7.3 13.3-15 0-2.2-.6-3.7-2.3-3.7zm51.3-6.7c-1.7 0-3-.6-4.2-1.9 2.4-1.5 4.1-4.8 4.1-7.8 0-3.1-1.8-6.1-6.8-6.1s-8.3 2.8-8.3 8.2c0 13.3 20.5 15.2 20.5 34.8 0 15.3-12.3 22.7-25.6 22.7-10.4 0-19.3-4.5-19.3-15.7 0-9.8 7-14.9 13.3-14.9 3.1 0 7.7 1.3 8 6-4.9 0-10.7 2.3-10.7 8.5 0 4.5 2.9 8.7 8.7 8.7 6.1 0 10.6-4.4 10.6-12 0-15.6-18.6-21.1-18.6-34.5 0-9.5 9.3-16.3 21-16.3 4.3 0 14.6.9 14.6 10.9.1 5.5-2.8 9.4-7.3 9.4zm36.2 10.3c0-2.3-.8-3.7-2.5-3.7-5.7 0-11.7 16.6-11.7 26.7 0 6.2 2.2 7.6 6.6 7.6 7.1 0 11.6-4 16.1-9.2h3.1c-5.2 8.3-12.9 16.8-25 16.8-8.5 0-14.2-4.2-14.2-14.5 0-10.6 6-32.3 24.5-32.3 8.1 0 10.1 4.2 10.1 8.3 0 4.4-2.2 6.7-4.8 6.7-1 0-2.1-.4-3.1-1.1.5-1.9.9-3.6.9-5.3zm27.7-7.6l-1.2 5.7c3.1-2.7 6.7-5.7 11-5.7 4.1 0 6.3 3.3 6.3 6.9 0 3.1-2.1 6.7-6.6 6.7-5.1 0-2.5-6-5.3-6-2.7 0-4.4 1.4-6.7 3.4l-7.2 34.6h-13.1l9.6-45.4 13.2-.2zm34.2 0l-6.6 30.9c-.3 1.2-.4 2.1-.4 2.9 0 2.5 1.2 3.3 3.7 3.3 3.5 0 6.9-3.4 8.1-8h3.8c-5.2 14.8-14.2 16.8-19.1 16.8-5.5 0-9.7-3.2-9.7-10.9 0-1.8.3-3.7.7-5.9l6.2-29.2 13.3.1zm-4.1-19.4c4 0 7.2 3.2 7.2 7.2s-3.2 7.1-7.2 7.1-7.1-3.1-7.1-7.1c-.1-4 3.2-7.2 7.1-7.2zm29.1 16l-1.5 6.9c2.6-2.3 6.1-3.9 10.7-3.9 6.2 0 11.1 3.5 11.1 14.4 0 12.2-4.7 32.1-22.3 32.1-4.5 0-6.8-1.6-7.7-3.2l-4.7 22.1-13.7 3.2 15.2-71.5 12.9-.1zm7.8 17c0-7-2.9-7.5-4.5-7.5-2 0-4.5 1.6-6.3 4.4l-5.4 25.5c.4 1 1.4 2.1 3.4 2.1 9.7 0 12.8-15.9 12.8-24.5zm27.8 17.3c-.3 1.1-.5 2.2-.5 3.1 0 1.9.7 3.2 3.1 3.2.7 0 1.7 0 2.4-.3-2.5 7.8-6.6 8.9-9.6 8.9-6.4 0-9.1-4.4-9.1-10.3 0-1.6.2-3.1.6-4.8l5.8-27.2h-3l.7-3.6h3L528 366l13.4-1.9s-1.4 6.2-3.1 14.4h5.5l-.7 3.6h-5.5l-5.7 27.4z"/> 533</svg> 534</a> 535 <div class="navigation toc"> 536 <div class="button"> 537 Table of Contents 538 </div> 539 <div class="contents menu"> 540 <a href="#overview">Overview</a> 541 <a href="#installation">Installation</a> 542 <a href="#usage">Usage</a> 543 <a href="#literate">Literate CoffeeScript</a> 544 <a href="#language">Language Reference</a> 545 <a href="#literals">Literals: Functions, Objects and Arrays</a> 546 <a href="#lexical-scope">Lexical Scoping and Variable Safety</a> 547 <a href="#conditionals">If, Else, Unless, and Conditional Assignment</a> 548 <a href="#splats">Splatsâ¦</a> 549 <a href="#loops">Loops and Comprehensions</a> 550 <a href="#slices">Array Slicing and Splicing</a> 551 <a href="#expressions">Everything is an Expression</a> 552 <a href="#operators">Operators and Aliases</a> 553 <a href="#existential-operator">Existential Operator</a> 554 <a href="#classes">Classes, Inheritance, and Super</a> 555 <a href="#destructuring">Destructuring Assignment</a> 556 <a href="#fat-arrow">Bound and Generator Functions</a> 557 <a href="#embedded">Embedded JavaScript</a> 558 <a href="#switch">Switch/When/Else</a> 559 <a href="#try-catch">Try/Catch/Finally</a> 560 <a href="#comparisons">Chained Comparisons</a> 561 <a href="#strings">String Interpolation, Block Strings, and Block Comments</a> 562 <a href="#tagged-template-literals">Tagged Template Literals</a> 563 <a href="#regexes">Block Regular Expressions</a> 564 <a href="#modules">Modules</a> 565 <a href="#cake">Cake, and Cakefiles</a> 566 <a href="#source-maps">Source Maps</a> 567 <a href="#scripts">"text/coffeescript" Script Tags</a> 568 <a href="#resources">Books, Screencasts, Examples and Resources</a> 569 <a href="#changelog">Change Log</a> 570 </div> 571 </div> 572 <div class="navigation try"> 573 <div class="button"> 574 Try CoffeeScript 575 <div class="repl_bridge"></div> 576 </div> 577 <div class="contents repl_wrapper"> 578 <div class="code"> 579 <div class="screenshadow tl"></div> 580 <div class="screenshadow tr"></div> 581 <div class="screenshadow bl"></div> 582 <div class="screenshadow br"></div> 583 <div id="repl_source_wrap"> 584 <textarea id="repl_source" rows="100" spellcheck="false">alert "Hello CoffeeScript!"</textarea> 585 </div> 586 <div id="repl_results_wrap"><pre id="repl_results"></pre></div> 587 <div class="minibutton dark run" title="Ctrl-Enter">Run</div> 588 <a class="minibutton permalink" id="repl_permalink">Link</a> 589 <br class="clear" /> 590 </div> 591 </div> 592 </div> 593 <div class="navigation annotated"> 594 <div class="button"> 595 Annotated Source 596 </div> 597 <div class="contents menu"> 598 <a href="annotated-source/grammar.html">Grammar Rules â src/grammar</a> 599 <a href="annotated-source/lexer.html">Lexing Tokens â src/lexer</a> 600 <a href="annotated-source/rewriter.html">The Rewriter â src/rewriter</a> 601 <a href="annotated-source/nodes.html">The Syntax Tree â src/nodes</a> 602 <a href="annotated-source/scope.html">Lexical Scope â src/scope</a> 603 <a href="annotated-source/helpers.html">Helpers & Utility Functions â src/helpers</a> 604 <a href="annotated-source/coffee-script.html">The CoffeeScript Module â src/coffee-script</a> 605 <a href="annotated-source/cake.html">Cake & Cakefiles â src/cake</a> 606 <a href="annotated-source/command.html">âcoffeeâ Command-Line Utility â src/command</a> 607 <a href="annotated-source/optparse.html">Option Parsing â src/optparse</a> 608 <a href="annotated-source/repl.html">Interactive REPL â src/repl</a> 609 <a href="annotated-source/sourcemap.html">Source Maps â src/sourcemap</a> 610 </div> 611 </div> 612</div> 613 614<div id="top" class="container"> 615 <span class="bookmark" id="overview"></span> 616 <p><strong>CoffeeScript is a little language that compiles into JavaScript.</strong> Underneath that awkward Java-esque patina, JavaScript has always had a gorgeous heart. CoffeeScript is an attempt to expose the good parts of JavaScript in a simple way.</p> 617<p>The golden rule of CoffeeScript is: <em>âItâs just JavaScriptâ</em>. The code compiles one-to-one into the equivalent JS, and there is no interpretation at runtime. You can use any existing JavaScript library seamlessly from C
617offeeScript (and vice-versa). The compiled output is readable, pretty-printed, and tends to run as fast or faster than the equivalent handwritten JavaScript.</p> 618<p>The CoffeeScript compiler goes to great lengths to generate output JavaScript that runs in every JavaScript runtime, but there are exceptions. Use <a href="#generator-functions">generator functions</a>, <a href="#generator-iteration"><code>forâ¦from</code></a>, or <a href="#tagged-template-literals">tagged template literals</a> only if you know that your <a href="http://kangax.github.io/compat-table/es6/">target runtimes can support them</a>. If you use <a href="#modules">modules</a>, you will need to <a href="#modules-note">use an additional tool to resolve them</a>.</p> 619<p><strong>Latest 1.x Version:</strong> <a href="https://github.com/jashkenas/coffeescript/tarball/1.12.7">1.12.7</a></p> 620<blockquote class="uneditable-code-block"><pre><code class="language-bash">npm install -g coffeescript 621</code></pre> 622</blockquote><p><strong>Upgrade to CoffeeScript 2!</strong> It adds support for <a href="/#classes">ES2015 classes</a>, <a href="/#async-functions"><code>async</code>/<code>await</code></a>, <a href="/#jsx">JSX</a>, <span class="nowrap"><a href="/#splats">object rest/spread syntax</a></span>, and <a href="/#coffeescript-2">JavaScript generated using modern syntax</a>. <a href="/announcing-coffeescript-2/">Learn more</a>.</p> 623 624 <h2>Overview</h2> 625<p><em>CoffeeScript on the left, compiled JavaScript output on the right.</em></p> 626<div class='code'><pre><code><span class="comment"># Assignment:</span> 627number = <span class="number">42</span> 628opposite = <span class="literal">true</span> 629 630<span class="comment"># Conditions:</span> 631number = <span class="number">-42</span> <span class="keyword">if</span> opposite 632 633<span class="comment"># Functions:</span> 634<span class="function"><span class="title">square</span> = <span class="params">(x)</span> -></span> x * x 635 636<span class="comment"># Arrays:</span> 637list = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>] 638 639<span class="comment"># Objects:</span> 640math = 641 root: Math.sqrt 642 square: square 643 cube: <span class="function"><span class="params">(x)</span> -></span> x * square x 644 645<span class="comment"># Splats:</span> 646<span class="function"><span class="title">race</span> = <span class="params">(winner, runners...)</span> -></span> 647 <span class="built_in">print</span> winner, runners 648 649<span class="comment"># Existence:</span> 650alert <span class="string">"I knew it!"</span> <span class="keyword">if</span> elvis? 651 652<span class="comment"># Array comprehensions:</span> 653cubes = (math.cube num <span class="keyword">for</span> num <span class="keyword">in</span> list) 654</code></pre><pre><code><span class="keyword">var</span> cubes, list, math, num, number, opposite, race, square, 655 slice = [].slice; 656 657number = <span class="number">42</span>; 658 659opposite = <span class="literal">true</span>; 660 661<span class="keyword">if</span> (opposite) { 662 number = <span class="number">-42</span>; 663} 664 665square = <span class="function"><span class="keyword">function</span>(<span class="params">x</span>
665) </span>{ 666 <span class="keyword">return</span> x * x; 667}; 668 669list = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]; 670 671math = { 672 <span class="attr">root</span>: <span class="built_in">Math</span>.sqrt, 673 <span class="attr">square</span>: square, 674 <span class="attr">cube</span>: <span class="function"><span class="keyword">function</span>(<span class="params">x</span>) </span>{ 675 <span class="keyword">return</span> x * square(x); 676 } 677}; 678 679race = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>{ 680 <span class="keyword">var</span> runners, winner; 681 winner = <span class="built_in">arguments</span>[<span class="number">0</span>], runners = <span class="number">2</span> <= <span class="built_in">arguments</span>.length ? slice.call(<span class="built_in">arguments</span>, <span class="number">1</span>) : []; 682 <span class="keyword">return</span> print(winner, runners); 683}; 684 685<span class="keyword">if</span> (<span class="keyword">typeof</span> elvis !== <span class="string">"undefined"</span> && elvis !== <span class="literal">null</span>) { 686 alert(<span class="string">"I knew it!"</span>); 687} 688 689cubes = (<span class="function"><span class="keyword">function</span>(<span class="params"></span>
689) </span>{ 690 <span class="keyword">var</span> i, len, results; 691 results = []; 692 <span class="keyword">for</span> (i = <span class="number">0</span>, len = list.length; i < len; i++) { 693 num = list[i]; 694 results.push(math.cube(num)); 695 } 696 <span class="keyword">return</span> results; 697})(); 698</code></pre>
698<script>window.example1 = "# Assignment:\nnumber = 42\nopposite = true\n\n# Conditions:\nnumber = -42 if opposite\n\n# Functions:\nsquare = (x) -> x * x\n\n# Arrays:\nlist = [1, 2, 3, 4, 5]\n\n# Objects:\nmath =\n root: Math.sqrt\n square: square\n cube: (x) -> x * square x\n\n# Splats:\nrace = (winner, runners...) ->\n print winner, runners\n\n# Existence:\nalert \"I knew it!\" if elvis?\n\n# Array comprehensions:\ncubes = (math.cube num for num in list)\n"</script>
698<div class="minibutton ok" onclick="javascript: var cubes, list, math, num, number, opposite, race, square, 699 slice = [].slice; 700 701number = 42; 702 703opposite = true; 704 705if (opposite) { 706 number = -42; 707} 708 709square = function(x) { 710 return x * x; 711}; 712 713list = [1, 2, 3, 4, 5]; 714 715math = { 716 root: Math.sqrt, 717 square: square, 718 cube: function(x) { 719 return x * square(x); 720 } 721}; 722 723race = function() { 724 var runners, winner; 725 winner = arguments[0], runners = 2 <= arguments.length ? slice.call(arguments, 1) : []; 726 return print(winner, runners); 727}; 728 729if (typeof elvis !== "undefined" && elvis !== null) { 730 alert("I knew it!"); 731} 732 733cubes = (function() { 734 var i, len, results; 735 results = []; 736 for (i = 0, len = list.length; i < len; i++) { 737 num = list[i]; 738 results.push(math.cube(num)); 739 } 740 return results; 741})(); 742;alert(cubes);">run: cubes</div><br class='clear' /></div> 743 <span class="bookmark" id="installation"></span> 744 <h2>Installation</h2> 745<p>The command-line version of <code>coffee</code> is available as a <a href="https://nodejs.org/">Node.js</a> utility. The <a href="browser-compiler/coffee-script.js">core compiler</a> however, does not depend on Node, and can be run in any JavaScript environment, or in the browser (see <a href="#try">Try CoffeeScript</a>).</p> 746<p>To install, first make sure you have a working copy of the latest stable version of <a href="https://nodejs.org/">Node.js</a>. You can then install CoffeeScript globally with <a href="https://www.npmjs.com/">npm</a>:</p> 747<blockquote class="uneditable-code-block"><pre><code class="language-bash">npm install --global coffeescript 748</code></pre> 749</blockquote><p>This will make the <code>coffee</code> and <code>cake</code> commands available globally.</p> 750<p>When you need CoffeeScript as a dependency of a project, within that projectâs folder you can install it locally:</p> 751<blockquote class="uneditable-code-block"><pre><code class="language-bash">npm install --save coffeescript 752</code></pre> 753</blockquote><p>The <code>coffee</code> and <code>cake</code> commands will first look in the current folder to see if CoffeeScript is installed locally, and use that version if so. This allows different versions of CoffeeScript to be installed globally and locally.</p> 754 755 <span class="bookmark" id="usage"></span> 756 <h2>Usage</h2> 757<p>Once installed, you should have access to the <code>coffee</code> command, which can execute scripts, compile <code>.coffee</code> files into <code>.js</code>, and provide an interactive REPL. The <code>coffee</code> command takes the following options:</p> 758<table> 759<thead> 760<tr> 761<th>Option</th> 762<th>Description</th> 763</tr> 764</thead> 765<tbody> 766<tr> 767<td><code>-c, --compile</code></td> 768<td>Compile a <code>.coffee</code> script into a <code>.js</code> JavaScript file of the same name.</td> 769</tr> 770<tr> 771<td><code>-m, --map</code></td> 772<td>Generate source maps alongside the compiled JavaScript files. Adds <code>sourceMappingURL</code> directives to the JavaScript as well.</td> 773</tr> 774<tr> 775<td><code>-M, --inline-map</code></td> 776<td>Just like <code>--map</code>, but include the source map directly in the compiled JavaScript files, rather than in a separate file.</td> 777</tr> 778<tr> 779<td><code>-i, --interactive</code></td> 780<td>Launch an interactive CoffeeScript session to try short snippets. Identical to calling <code>coffee</code> with no arguments.</td> 781</tr> 782<tr> 783<td><code>-o, --output [DIR]</code></td> 784<td>Write out all compiled JavaScript files into the specified directory. Use in conjunction with <code>--compile</code> or <code>--watch</code>.</td> 785</tr> 786<tr> 787<td><code>-w, --watch</code></td> 788<td>Watch files for changes, rerunning the specified command when any file is updated.</td> 789</tr> 790<tr> 791<td><code>-p, --print</code></td> 792<td>Instead of writing out the JavaScript as a file, print it directly to <strong>stdout</strong>.</td> 793</tr> 794<tr> 795<td><code>-s, --stdio</code></td> 796<td>Pipe in CoffeeScript to STDIN and get back JavaScript over STDOUT. Good for use with processes written in other languages. An example:<br><code>cat src/cake.coffee | coffee -sc</code></td> 797</tr> 798<tr> 799<td><code>-l, --literate</code></td> 800<td>Parses the code as Literate CoffeeScript. You only need to specify this when passing in code directly over <strong>stdio</strong>, or using some sort of extension-less file name.</td> 801</tr> 802<tr> 803<td><code>-e, --eval</code></td> 804<td>Compile and print a little snippet of CoffeeScript directly from the command line. For example:<br><code>coffee -e "console.log num for num in [10..1]"</code></td> 805</tr> 806<tr> 807<td><code>-r, --require [MODULE]</code>â</td> 808<td><code>require()</code> the given module before starting the REPL or evaluating the code given with the <code>--eval</code> flag.</td> 809</tr> 810<tr> 811<td><code>-b, --bare</code></td> 812<td>Compile the JavaScript without the <a href="#lexical-scope">top-level function safety wrapper</a>.</td> 813</tr> 814<tr> 815<td><code>-t, --tokens</code></td> 816<td>Instead of parsing the CoffeeScript, just lex it, and print out the token stream. Used for debugging the compiler.</td> 817</tr> 818<tr> 819<td><code>-n, --nodes</code></td> 820<td>Instead of compiling the CoffeeScript, just lex and parse it, and print out the parse tree. Used for debugging the compiler.</td> 821</tr> 822<tr> 823<td><code>--nodejs</code></td> 824<td>The <code>node</code> executable has some useful options you can set, such as <code>--debug</code>, <code>--debug-brk</code>, <code>--max-stack-size</code>, and <code>--expose-gc</code>. Use this flag to forward options directly to Node.js. To pass multiple flags, use <code>--nodejs</code> multiple times.</td> 825</tr> 826<tr> 827<td><code>--no-header</code></td> 828<td>Suppress the âGenerated by CoffeeScriptâ header.</td> 829</tr> 830</tbody> 831</table> 832<h3>Examples:</h3> 833<ul> 834<li>Compile a directory tree of <code>.coffee</code> files in <code>src</code> into a parallel tree of <code>.js</code> files in <code>lib</code>:<br> 835<code>coffee --compile --output lib/ src/</code></li> 836<li>Watch a file for changes, and recompile it every time the file is saved:<br> 837<code>coffee --watch --compile experimental.coffee</code></li> 838<li>Concatenate a list of files into a single script:<br> 839<code>coffee --join project.js --compile src/*.coffee</code></li> 840<li>Print out the compiled JS from a one-liner:<br> 841<code>coffee -bpe "alert i for i in [0..10]"</code></li> 842<li>All together now, watch and recompile an entire project as you work on it:<br> 843<code>coffee -o lib/ -cw src/</code></li> 844<li>Start the CoffeeScript REPL (<code>Ctrl-D</code> to exit, <code>Ctrl-V</code>for multi-line):<br> 845<code>coffee</code></li> 846</ul> 847 848 <span class="bookmark" id="literate"></span> 849 <h2>Literate CoffeeScript</h2> 850<p>Besides being used as an ordinary programming language, CoffeeScript may al
850so be written in âliterateâ mode. If you name your file with a <code>.litcoffee</code> extension, you can write it as a Markdown document â a document that also happens to be executable CoffeeScript code. The compiler will treat any indented blocks (Markdownâs way of indicating source code) as code, and ignore the rest as comments.</p> 851<p>Just for kicks, a little bit of the compiler is currently implemented in this fashion: See it <a href="https://gist.github.com/jashkenas/3fc3c1a8b1009c00d9df">as a document</a>, <a href="https://raw.githubusercontent.com/jashkenas/coffeescript/master/src/scope.litcoffee">raw</a>, and <a href="http://cl.ly/LxEu">properly highlighted in a text editor</a>.</p> 852 853 <span class="bookmark" id="language"></span> 854 <h2>Language Reference</h2> 855<p><em>This reference is structured so that it can be read from top to bottom, if you like. Later sections use ideas and syntax previously introduced. Familiarity with JavaScript is assumed. In all of the following examples, the source CoffeeScript is provided on the left, and the direct compilation into JavaScript is on the right.</em></p> 856<p><em>Many of the examples can be run (where it makes sense) by pressing the <strong>run</strong> button on the right, and can be loaded into the âTry CoffeeScriptâ console by pressing the <strong>load</strong> button on the left.</em></p> 857<p>First, the basics: CoffeeScript uses significant whitespace to delimit blocks of code. You donât need to use semicolons <code>;</code> to terminate expressions, ending the line will do just as well (although semicolons can still be used to fit multiple expressions onto a single line). Instead of using curly braces <code>{ }</code> to surround blocks of code in <a href="#literals">functions</a>, <a href="#conditionals">if-statements</a>, <a href="#switch">switch</a>, and <a href="#try-catch">try/catch</a>, use indentation.</p> 858<p>You donât need to use parentheses to invoke a function if youâre passing arguments. The implicit call wraps forward to the end of the line or block expression.<br> 859<code>console.log sys.inspect object</code> â <code>console.log(sys.inspect(object));</code></p> 860 861 <span class="bookmark" id="literals"></span> 862 <h2>Functions</h2> 863<p>Functions are defined by an optional list of parameters in parentheses, an arrow, and the function body. The empty function looks like this: <code>-></code></p> 864<div class='code'><pre><code><span class="function"><span class="title">square</span> = <span class="params">(x)</span> -></span> x * x 865<span class="function"><span class="title">cube</span> = <span class="params">(x)</span> -></span> square(x) * x 866</code></pre><pre><code><span class="keyword">var</span> cube, square; 867 868square = <span class="function"><span class="keyword">function</span>(<span class="params">x</span>
868) </span>{ 869 <span class="keyword">return</span> x * x; 870}; 871 872cube = <span class="function"><span class="keyword">function</span>(<span class="params">x</span>) </span>{ 873 <span class="keyword">return</span> square(x) * x; 874}; 875</code></pre>
875<script>window.example1 = "square = (x) -> x * x\ncube = (x) -> square(x) * x\n"</script>
875<div class='minibutton load' onclick='javascript: loadConsole(example1);'>load</div><div class="minibutton ok" onclick="javascript: var cube, square; 876 877square = function(x) { 878 return x * x; 879}; 880 881cube = function(x) { 882 return square(x) * x; 883}; 884;alert(cube(5));">run: cube(5)</div><br class='clear' /></div><p>Functions may also have default values for arguments, which will be used if the incoming argument is missing (<code>null</code> or <code>undefined</code>).</p> 885<div class='code'><pre><code><span class="function"><span class="title">fill</span> = <span class="params">(container, liquid = <span class="string">"coffee"</span>)</span> -></span> 886 <span class="string">"Filling the <span class="subst">#{container}</span> with <span class="subst">#{liquid}</span>..."</span> 887</code></pre><pre><code><span class="keyword">var</span> fill; 888 889fill = <span class="function"><span class="keyword">function</span>(<span class="params">container, liquid</span>) </span>{ 890 <span class="keyword">if</span> (liquid == <span class="literal">null</span>) { 891 liquid = <span class="string">"coffee"</span>; 892 } 893 <span class="keyword">return</span> <span class="string">"Filling the "</span> + container + <span class="string">" with "</span> + liquid + <span class="string">"..."</span>; 894}; 895</code></pre>
895<script>window.example2 = "fill = (container, liquid = \"coffee\") ->\n \"Filling the #{container} with #{liquid}...\"\n"</script>
895<div class='minibutton load' onclick='javascript: loadConsole(example2);'>load</div><div class="minibutton ok" onclick="javascript: var fill; 896 897fill = function(container, liquid) { 898 if (liquid == null) { 899 liquid = "coffee"; 900 } 901 return "Filling the " + container + " with " + liquid + "..."; 902}; 903;alert(fill("cup"));">run: fill("cup")</div><br class='clear' /></div> 904 <span class="bookmark" id="objects-and-arrays"></span> 905 <h2>Objects and Arrays</h2> 906<p>The CoffeeScript literals for objects and arrays look very similar to their JavaScript cousins. When each property is listed on its own line, the commas are optional. Objects may be created using indentation instead of explicit braces, similar to <a href="http://yaml.org">YAML</a>.</p> 907<div class='code'><pre><code>song = [<span class="string">"do"</span>, <span class="string">"re"</span>, <span class="string">"mi"</span>, <span class="string">"fa"</span>, <span class="string">"so"</span>] 908 909singers = {Jagger: <span class="string">"Rock"</span>, Elvis: <span class="string">"Roll"</span>} 910 911bitlist = [ 912 <span class="number">1</span>, <span class="number">0</span>, <span class="number">1</span> 913 <span class="number">0</span>, <span class="number">0</span>, <span class="number">1</span> 914 <span class="number">1</span>, <span class="number">1</span>, <span class="number">0</span> 915] 916 917kids = 918 brother: 919 name: <span class="string">"Max"</span> 920 age: <span class="number">11</span> 921 sister: 922 name: <span class="string">"Ida"</span> 923 age: <span class="number">9</span> 924</code></pre><pre><code><span class="keyword">var</span> bitlist, kids, singers, song; 925 926song = [<span class="string">"do"</span>, <span class="string">"re"</span>, <span class="string">"mi"</span>, <span class="string">"fa"</span>, <span class="string">"so"</span>]; 927 928singers = { 929 <span class="attr">Jagger</span>: <span class="string">"Rock"</span>, 930 <span class="attr">Elvis</span>: <span class="string">"Roll"</span> 931}; 932 933bitlist = [<span class="number">1</span>, <span class="number">0</span>, <span class="number">1</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">0</span>]; 934 935kids = { 936 <span class="attr">brother</span>: { 937 <span class="attr">name</span>: <span class="string">"Max"</span>, 938 <span class="attr">age</span>: <span class="number">11</span> 939 }, 940 <span class="attr">sister</span>: { 941 <span class="attr">name</span>: <span class="string">"Ida"</span>, 942 <span class="attr">age</span>: <span class="number">9</span> 943 } 944}; 945</code></pre>
945<script>window.example1 = "song = [\"do\", \"re\", \"mi\", \"fa\", \"so\"]\n\nsingers = {Jagger: \"Rock\", Elvis: \"Roll\"}\n\nbitlist = [\n 1, 0, 1\n 0, 0, 1\n 1, 1, 0\n]\n\nkids =\n brother:\n name: \"Max\"\n age: 11\n sister:\n name: \"Ida\"\n age: 9\n"</script>
945<div class='minibutton load' onclick='javascript: loadConsole(example1);'>load</div><div class="minibutton ok" onclick="javascript: var bitlist, kids, singers, song; 946 947song = ["do", "re", "mi", "fa", "so"]; 948 949singers = { 950 Jagger: "Rock", 951 Elvis: "Roll" 952}; 953 954bitlist = [1, 0, 1, 0, 0, 1, 1, 1, 0]; 955 956kids = { 957 brother: { 958 name: "Max", 959 age: 11 960 }, 961 sister: { 962 name: "Ida", 963 age: 9 964 } 965}; 966;alert(song.join(" ⦠"));">run: song.join(" ⦠")</div><br class='clear' /></div><p>In JavaScript, you canât use reserved words, like <code>class</code>, as properties of an object, without quoting them as strings. CoffeeScript notices reserved words used as keys in objects and quotes them for you, so you donât have to worry about it (say, when using jQuery).</p> 967<div class='code'><pre><code>$('.account').attr class: 'active' 968 969log object.class 970</code></pre><pre><code>$(<span class="string">'.account'</span>).attr({ 971 <span class="string">"class"</span>: <span class="string">'active'</span> 972}); 973 974log(object[<span class="string">"class"</span>]); 975</code></pre>
975<script>window.example2 = "$('.account').attr class: 'active'\n\nlog object.class\n"</script>
975<div class='minibutton load' onclick='javascript: loadConsole(example2);'>load</div><br class='clear' /></div><p>CoffeeScript has a shortcut for creating objects when you want the key to be set with a variable of the same name.</p> 976<div class='code'><pre><code>name = <span class="string">"Michelangelo"</span> 977mask = <span class="string">"orange"</span> 978weapon = <span class="string">"nunchuks"</span> 979turtle = {name, mask, weapon} 980output = <span class="string">"<span class="subst">#{turtle.name}</span> wears an <span class="subst">#{turtle.mask}</span> mask. Watch out for his <span class="subst">#{turtle.weapon}</span>!"</span> 981</code></pre><pre><code><span class="keyword">var</span> mask, name, output, turtle, weapon; 982 983name = <span class="string">"Michelangelo"</span>; 984 985mask = <span class="string">"orange"</span>; 986 987weapon = <span class="string">"nunchuks"</span>; 988 989turtle = { 990 <span class="attr">name</span>: name, 991 <span class="attr">mask</span>: mask, 992 <span class="attr">weapon</span>: weapon 993}; 994 995output = turtle.name + <span class="string">" wears an "</span> + turtle.mask + <span class="string">" mask. Watch out for his "</span> + turtle.weapon + <span class="string">"!"</span>; 996</code></pre>
996<script>window.example3 = "name = \"Michelangelo\"\nmask = \"orange\"\nweapon = \"nunchuks\"\nturtle = {name, mask, weapon}\noutput = \"#{turtle.name} wears an #{turtle.mask} mask. Watch out for his #{turtle.weapon}!\"\n"</script>
996<div class='minibutton load' onclick='javascript: loadConsole(example3);'>load</div><br class='clear' /></div> 997 <span class="bookmark" id="lexical-scope"></span> 998 <h2>Lexical Scoping and Variable Safety</h2> 999<p>The CoffeeScript compiler takes care to make sure that all of your variables are properly declared within lexical scope â you never need to write <code>var</code> yourself.</p> 1000<div class='code'><pre><code>outer = <span class="number">1</span> 1001<span class="function"><span class="title">changeNumbers</span> = -></span> 1002 inner = <span class="number">-1</span> 1003 outer = <span class="number">10</span> 1004inner = changeNumbers() 1005</code></pre><pre><code><span class="keyword">var</span> changeNumbers, inner, outer; 1006 1007outer = <span class="number">1</span>; 1008 1009changeNumbers = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>{ 1010 <span class="keyword">var</span> inner; 1011 inner = <span class="number">-1</span>; 1012 <span class="keyword">return</span> outer = <span class="number">10</span>; 1013}; 1014 1015inner = changeNumbers(); 1016</code></pre>
1016<script>window.example1 = "outer = 1\nchangeNumbers = ->\n inner = -1\n outer = 10\ninner = changeNumbers()\n"</script>
1016<div class='minibutton load' onclick='javascript: loadConsole(example1);'>load</div><div class="minibutton ok" onclick="javascript: var changeNumbers, inner, outer; 1017 1018outer = 1; 1019 1020changeNumbers = function() { 1021 var inner; 1022 inner = -1; 1023 return outer = 10; 1024}; 1025 1026inner = changeNumbers(); 1027;alert(inner);">run: inner</div><br class='clear' /></div><p>Notice how all of the variable declarations have been pushed up to the top of the closest scope, the first time they appear. <strong>outer</strong> is not redeclared within the inner function, because itâs already in scope; <strong>inner</strong> within the function, on the other hand, should not be able to change the value of the external variable of the same name, and therefore has a declaration of its own.</p> 1028<p>This behavior is effectively identical to Rubyâs scope for local variables. Because you donât have direct access to the <code>var</code> keyword, itâs impossible to shadow an outer variable on purpose, you may only refer to it. So be careful that youâre not reusing the name of an external variable accidentally, if youâre writing a deeply nested function.</p> 1029<p>Although suppressed within this documentation for clarity, all CoffeeScript output is wrapped in an anonymous function: <code>(function(){ ⦠})();</code> This safety wrapper, combined with the automatic generation of the <code>var</code> keyword, make it exceedingly difficult to pollute the global namespace by accident.</p> 1030<p>If youâd like to create top-level variables for other scripts to use, attach them as properties on <strong>window</strong>; attach them as properties on the <strong>exports</strong> object in CommonJS; or use an <a href="#modules"><code>export</code> statement</a>. If youâre targeting both CommonJS and the browser, the <strong>existential operator</strong> (covered below), gives you a reliable way to figure out where to add them: <code>exports ? this</code></p> 1031 1032 <span class="bookmark" id="conditionals"></span> 1033 <h2>If, Else, Unless, and Conditional Assignment</h2> 1034<p><strong>If/else</strong> statements can be written without the use of parentheses and curly brackets. As with functions and other block expressions, multi-line conditionals are delimited by indentation. Thereâs also a handy postfix form, with the <code>if</code> or <code>unless</code> at the end.</p> 1035<p>CoffeeScript can compile <strong>if</strong> statements into JavaScript expressions, using the ternary operator when possible, and closure wrapping otherwise. There is no explicit ternary statement in CoffeeScript â you simply use a regular <strong>if</strong> statement on a single line.</p> 1036<div class='code'><pre><code>mood = greatlyImproved <span class="keyword">if</span> singing 1037 1038<span class="keyword">if</span> happy <span class="keyword">and</span> knowsIt 1039 clapsHands() 1040 chaChaCha() 1041<span class="keyword">else</span> 1042 showIt() 1043 1044date = <span class="keyword">if</span> friday <span class="keyword">then</span> sue <span class="keyword">else</span> jill 1045</code></pre><pre><code><span class="keyword">var</span> date, mood; 1046 1047<span class="keyword">if</span> (singing) { 1048 mood = greatlyImproved; 1049} 1050 1051<span class="keyword">if</span> (happy && knowsIt) { 1052 clapsHands(); 1053 chaChaCha(); 1054} <span class="keyword">else</span> { 1055 showIt(); 1056} 1057 1058date = friday ? sue : jill; 1059</code></pre>
1059<script>window.example1 = "mood = greatlyImproved if singing\n\nif happy and knowsIt\n clapsHands()\n chaChaCha()\nelse\n showIt()\n\ndate = if friday then sue else jill\n"</script>
1059<div class='minibutton load' onclick='javascript: loadConsole(example1);'>load</div><br class='clear' /></div> 1060 <span class="bookmark" id="splats"></span> 1061 <h2>Splatsâ¦</h2> 1062<p>The JavaScript <strong>arguments object</strong> is a useful way to work with functions that accept variable numbers of arguments. CoffeeScript provides splats <code>...</code>, both for function definition as well as invocation, making variable numbers of arguments a little bit more palatable.</p> 1063<div class='code'><pre><code>gold = silver = rest = <span class="string">"unknown"</span> 1064<span class="function"> 1065<span class="title">awardMedals</span> = <span class="params">(first, second, others...)</span> -></span> 1066 gold = first 1067 silver = second 1068 rest = others 1069 1070contenders = [ 1071 <span class="string">"Michael Phelps"</span> 1072 <span class="string">"Liu Xiang"</span> 1073 <span class="string">"Yao Ming"</span> 1074 <span class="string">"Allyson Felix"</span> 1075 <span class="string">"Shawn Johnson"</span> 1076 <span class="string">"Roman Sebrle"</span> 1077 <span class="string">"Guo Jingjing"</span> 1078 <span class="string">"Tyson Gay"</span> 1079 <span class="string">"Asafa Powell"</span> 1080 <span class="string">"Usain Bolt"</span> 1081] 1082 1083awardMedals contenders... 1084 1085alert <span class="string">"Gold: "</span> + gold 1086alert <span class="string">"Silver: "</span> + silver 1087alert <span class="string">"The Field: "</span> + rest 1088</code></pre><pre><code><span class="keyword">var</span> awardMedals, contenders, gold, rest, silver, 1089 slice = [].slice; 1090 1091gold = silver = rest = <span class="string">"unknown"</span>; 1092 1093awardMedals = <span class="function"><span class="keyword">function</span>(<span class="params"></span>
1093) </span>{ 1094 <span class="keyword">var</span> first, others, second; 1095 first = <span class="built_in">arguments</span>[<span class="number">0</span>], second = <span class="built_in">arguments</span>[<span class="number">1</span>], others = <span class="number">3</span> <= <span class="built_in">arguments</span>.length ? slice.call(<span class="built_in">arguments</span>, <span class="number">2</span>) : []; 1096 gold = first; 1097 silver = second; 1098 <span class="keyword">return</span> rest = others; 1099}; 1100 1101contenders = [<span class="string">"Michael Phelps"</span>, <span class="string">"Liu Xiang"</span>, <span class="string">"Yao Ming"</span>, <span class="string">"Allyson Felix"</span>, <span class="string">"Shawn Johnson"</span>, <span class="string">"Roman Sebrle"</span>, <span class="string">"Guo Jingjing"</span>, <span class="string">"Tyson Gay"</span>, <span class="string">"Asafa Powell"</span>, <span class="string">"Usain Bolt"</span>]; 1102 1103awardMedals.apply(<span class="literal">null</span>, contenders); 1104 1105alert(<span class="string">"Gold: "</span> + gold); 1106 1107alert(<span class="string">"Silver: "</span> + silver); 1108 1109alert(<span class="string">"The Field: "</span> + rest); 1110</code></pre>
1110<script>window.example1 = "gold = silver = rest = \"unknown\"\n\nawardMedals = (first, second, others...) ->\n gold = first\n silver = second\n rest = others\n\ncontenders = [\n \"Michael Phelps\"\n \"Liu Xiang\"\n \"Yao Ming\"\n \"Allyson Felix\"\n \"Shawn Johnson\"\n \"Roman Sebrle\"\n \"Guo Jingjing\"\n \"Tyson Gay\"\n \"Asafa Powell\"\n \"Usain Bolt\"\n]\n\nawardMedals contenders...\n\nalert \"Gold: \" + gold\nalert \"Silver: \" + silver\nalert \"The Field: \" + rest\n"</script>
1110<div class='minibutton load' onclick='javascript: loadConsole(example1);'>load</div><div class="minibutton ok" onclick="javascript: var awardMedals, contenders, gold, rest, silver, 1111 slice = [].slice; 1112 1113gold = silver = rest = "unknown"; 1114 1115awardMedals = function() { 1116 var first, others, second; 1117 first = arguments[0], second = arguments[1], others = 3 <= arguments.length ? slice.call(arguments, 2) : []; 1118 gold = first; 1119 silver = second; 1120 return rest = others; 1121}; 1122 1123contenders = ["Michael Phelps", "Liu Xiang", "Yao Ming", "Allyson Felix", "Shawn Johnson", "Roman Sebrle", "Guo Jingjing", "Tyson Gay", "Asafa Powell", "Usain Bolt"]; 1124 1125awardMedals.apply(null, contenders); 1126 1127alert("Gold: " + gold); 1128 1129alert("Silver: " + silver); 1130 1131alert("The Field: " + rest); 1132;">run</div><br class='clear' /></div> 1133 <span class="bookmark" id="loops"></span> 1134 <h2>Loops and Comprehensions</h2> 1135<p>Most of the loops youâll write in CoffeeScript will be <strong>comprehensions</strong> over arrays, objects, and ranges. Comprehensions replace (and compile into) <strong>for</strong> loops, with optional guard clauses and the value of the current array index. Unlike for loops, array comprehensions are expressions, and can be returned and assigned.</p> 1136<div class='code'><pre><code><span class="comment"># Eat lunch.</span> 1137eat food <span class="keyword">for</span> food <span class="keyword">in</span> [<span class="string">'toast'</span>, <span class="string">'cheese'</span>, <span class="string">'wine'</span>] 1138 1139<span class="comment"># Fine five course dining.</span> 1140courses = [<span class="string">'greens'</span>, <span class="string">'caviar'</span>, <span class="string">'truffles'</span>, <span class="string">'roast'</span>, <span class="string">'cake'</span>] 1141menu i + <span class="number">1</span>, dish <span class="keyword">for</span> dish, i <span class="keyword">in</span> courses 1142 1143<span class="comment"># Health conscious meal.</span> 1144foods = [<span class="string">'broccoli'</span>, <span class="string">'spinach'</span>, <span class="string">'chocolate'</span>] 1145eat food <span class="keyword">for</span> food <span class="keyword">in</span> foods <span class="keyword">when</span> food <span class="keyword">isnt</span> <span class="string">'chocolate'</span> 1146</code></pre><pre><code><span class="keyword">var</span> courses, dish, food, foods, i, j, k, l, len, len1, len2, ref; 1147 1148ref = [<span class="string">'toast'</span>, <span class="string">'cheese'</span>, <span class="string">'wine'</span>]; 1149<span class="keyword">for</span> (j = <span class="number">0</span>, len = ref.length; j < len; j++) { 1150 food = ref[j]; 1151 eat(food); 1152} 1153 1154courses = [<span class="string">'greens'</span>, <span class="string">'caviar'</span>, <span class="string">'truffles'</span>, <span class="string">'roast'</span>, <span class="string">'cake'</span>]; 1155 1156<span class="keyword">for</span> (i = k = <span class="number">0</span>, len1 = courses.length; k < len1; i = ++k) { 1157 dish = courses[i]; 1158 menu(i + <span class="number">1</span>, dish); 1159} 1160 1161foods = [<span class="string">'broccoli'</span>, <span class="string">'spinach'</span>, <span class="string">'chocolate'</span>]; 1162 1163<span class="keyword">for</span> (l = <span class="number">0</span>, len2 = foods.length; l < len2; l++) { 1164 food = foods[l]; 1165 <span class="keyword">if</span> (food !== <span class="string">'chocolate'</span>) { 1166 eat(food); 1167 } 1168} 1169</code></pre>
1169<script>window.example1 = "# Eat lunch.\neat food for food in ['toast', 'cheese', 'wine']\n\n# Fine five course dining.\ncourses = ['greens', 'caviar', 'truffles', 'roast', 'cake']\nmenu i + 1, dish for dish, i in courses\n\n# Health conscious meal.\nfoods = ['broccoli', 'spinach', 'chocolate']\neat food for food in foods when food isnt 'chocolate'\n"</script>
1169<div class='minibutton load' onclick='javascript: loadConsole(example1);'>load</div><br class='clear' /></div><p>Comprehensions should be able to handle most places where you otherwise would use a loop, <strong>each</strong>/<strong>forEach</strong>, <strong>map</strong>, or <strong>select</strong>/<strong>filter</strong>, for example:<br> 1170<code>shortNames = (name for name in list when name.length < 5)</code><br> 1171If you know the start and end of your loop, or would like to step through in fixed-size increments, you can use a range to specify the start and end of your comprehension.</p> 1172<div class='code'><pre><code>countdown = (num <span class="keyword">for</span> num <span class="keyword">in</span> [<span class="number">10.</span><span class="number">.1</span>]) 1173</code></pre><pre><code><span class="keyword">var</span> countdown, num; 1174 1175countdown = (<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>{ 1176 <span class="keyword">var</span> i, results; 1177 results = []; 1178 <span class="keyword">for</span> (num = i = <span class="number">10</span>; i >= <span class="number">1</span>; num = --i) { 1179 results.push(num); 1180 } 1181 <span class="keyword">return</span> results; 1182})(); 1183</code></pre>
1183<script>window.example2 = "countdown = (num for num in [10..1])\n"</script>
1183<div class='minibutton load' onclick='javascript: loadConsole(example2);'>load</div><div class="minibutton ok" onclick="javascript: var countdown, num; 1184 1185countdown = (function() { 1186 var i, results; 1187 results = []; 1188 for (num = i = 10; i >= 1; num = --i) { 1189 results.push(num); 1190 } 1191 return results; 1192})(); 1193;alert(countdown);">run: countdown</div><br class='clear' /></div><p>Note how because we are assigning the value of the comprehensions to a variable in the example above, CoffeeScript is collecting the result of each iteration into an array. Sometimes functions end with loops that are intended to run only for their side-effects. Be careful that youâre not accidentally returning the results of the comprehension in these cases, by adding a meaningful return value â like <code>true</code> â or <code>null</code>, to the bottom of your function.</p> 1194<p>To step through a range comprehension in fixed-size chunks, use <code>by</code>, for example: 1195<code>evens = (x for x in [0..10] by 2)</code></p> 1196<p>If you donât need the current iteration value you may omit it: 1197<code>browser.closeCurrentTab() for [0...count]</code></p> 1198<p>Comprehensions can also be used to iterate over the keys and values in an object. Use <code>of</code> to signal comprehension over the properties of an object instead of the values in an array.</p> 1199<div class='code'><pre><code>yearsOld = max: <span class="number">10</span>, ida: <span class="number">9</span>, tim: <span class="number">11</span> 1200 1201ages = <span class="keyword">for</span> child, age <span class="keyword">of</span> yearsOld 1202 <span class="string">"<span class="subst">#{child}</span> is <span class="subst">#{age}</span>"</span> 1203</code></pre><pre><code><span class="keyword">var</span> age, ages, child, yearsOld; 1204 1205yearsOld = { 1206 <span class="attr">max</span>: <span class="number">10</span>, 1207 <span class="attr">ida</span>: <span class="number">9</span>, 1208 <span class="attr">tim</span>: <span class="number">11</span> 1209}; 1210 1211ages = (<span class="function"><span class="keyword">function</span>(<span class="params"></span>
1211) </span>{ 1212 <span class="keyword">var</span> results; 1213 results = []; 1214 <span class="keyword">for</span> (child <span class="keyword">in</span> yearsOld) { 1215 age = yearsOld[child]; 1216 results.push(child + <span class="string">" is "</span> + age); 1217 } 1218 <span class="keyword">return</span> results; 1219})(); 1220</code></pre>
1220<script>window.example3 = "yearsOld = max: 10, ida: 9, tim: 11\n\nages = for child, age of yearsOld\n \"#{child} is #{age}\"\n"</script>
1220<div class='minibutton load' onclick='javascript: loadConsole(example3);'>load</div><div class="minibutton ok" onclick="javascript: var age, ages, child, yearsOld; 1221 1222yearsOld = { 1223 max: 10, 1224 ida: 9, 1225 tim: 11 1226}; 1227 1228ages = (function() { 1229 var results; 1230 results = []; 1231 for (child in yearsOld) { 1232 age = yearsOld[child]; 1233 results.push(child + " is " + age); 1234 } 1235 return results; 1236})(); 1237;alert(ages.join(", "));">run: ages.join(", ")</div><br class='clear' /></div><p>If you would like to iterate over just the keys that are defined on the object itself, by adding a <code>hasOwnProperty</code> check to avoid properties that may be inherited from the prototype, use <code>for own key, value of object</code>.</p> 1238<p>To iterate a generator function, use <code>from</code>. See <a href="#generator-iteration">Generator Functions</a>.</p> 1239<p>The only low-level loop that CoffeeScript provides is the <strong>while</strong> loop. The main difference from JavaScript is that the <strong>while</strong> loop can be used as an expression, returning an array containing the result of each iteration through the loop.</p> 1240<div class='code'><pre><code><span class="comment"># Econ 101</span> 1241<span class="keyword">if</span> <span class="keyword">this</span>.studyingEconomics 1242 buy() <span class="keyword">while</span> supply > demand 1243 sell() <span class="keyword">until</span> supply > demand 1244 1245<span class="comment"># Nursery Rhyme</span> 1246num = <span class="number">6</span> 1247lyrics = <span class="keyword">while</span> num -= <span class="number">1</span> 1248 <span class="string">"<span class="subst">#{num}</span> little monkeys, jumping on the bed. 1249 One fell out and bumped his head."</span> 1250</code></pre><pre><code><span class="keyword">var</span> lyrics, num; 1251 1252<span class="keyword">if</span> (<span class="keyword">this</span>.studyingEconomics) { 1253 <span class="keyword">while</span> (supply > demand) { 1254 buy(); 1255 } 1256 <span class="keyword">while</span> (!(supply > demand)) { 1257 sell(); 1258 } 1259} 1260 1261num = <span class="number">6</span>; 1262 1263lyrics = (<span class="function"><span class="keyword">function</span>(<span class="params"></span>
1263) </span>{ 1264 <span class="keyword">var</span> results; 1265 results = []; 1266 <span class="keyword">while</span> (num -= <span class="number">1</span>) { 1267 results.push(num + <span class="string">" little monkeys, jumping on the bed. One fell out and bumped his head."</span>); 1268 } 1269 <span class="keyword">return</span> results; 1270})(); 1271</code></pre>
1271<script>window.example4 = "# Econ 101\nif this.studyingEconomics\n buy() while supply > demand\n sell() until supply > demand\n\n# Nursery Rhyme\nnum = 6\nlyrics = while num -= 1\n \"#{num} little monkeys, jumping on the bed.\n One fell out and bumped his head.\"\n"</script>
1271<div class='minibutton load' onclick='javascript: loadConsole(example4);'>load</div><div class="minibutton ok" onclick="javascript: var lyrics, num; 1272 1273if (this.studyingEconomics) { 1274 while (supply > demand) { 1275 buy(); 1276 } 1277 while (!(supply > demand)) { 1278 sell(); 1279 } 1280} 1281 1282num = 6; 1283 1284lyrics = (function() { 1285 var results; 1286 results = []; 1287 while (num -= 1) { 1288 results.push(num + " little monkeys, jumping on the bed. One fell out and bumped his head."); 1289 } 1290 return results; 1291})(); 1292;alert(lyrics.join("\n"));">run: lyrics.join("\n")</div><br class='clear' /></div><p>For readability, the <strong>until</strong> keyword is equivalent to <code>while not</code>, and the <strong>loop</strong> keyword is equivalent to <code>while true</code>.</p> 1293<p>When using a JavaScript loop to generate functions, itâs common to insert a closure wrapper in order to ensure that loop variables are closed over, and all the generated functions donât just share the final values. CoffeeScript provides the <code>do</code> keyword, which immediately invokes a passed function, forwarding any arguments.</p> 1294<div class='code'><pre><code><span class="keyword">for</span> filename <span class="keyword">in</span> list 1295 <span class="keyword">do</span> (filename) -> 1296 fs.readFile filename, <span class="function"><span class="params">(err, contents)</span> -></span> 1297 compile filename, contents.toString() 1298</code></pre><pre><code><span class="keyword">var</span> filename, fn, i, len; 1299 1300fn = <span class="function"><span class="keyword">function</span>(<span class="params">filename</span>
1300) </span>{ 1301 <span class="keyword">return</span> fs.readFile(filename, <span class="function"><span class="keyword">function</span>(<span class="params">err, contents</span>) </span>{ 1302 <span class="keyword">return</span> compile(filename, contents.toString()); 1303 }); 1304}; 1305<span class="keyword">for</span> (i = <span class="number">0</span>, len = list.length; i < len; i++) { 1306 filename = list[i]; 1307 fn(filename); 1308} 1309</code></pre>
1309<script>window.example5 = "for filename in list\n do (filename) ->\n fs.readFile filename, (err, contents) ->\n compile filename, contents.toString()\n"</script>
1309<div class='minibutton load' onclick='javascript: loadConsole(example5);'>load</div><br class='clear' /></div> 1310 <span class="bookmark" id="slices"></span> 1311 <h2>Array Slicing and Splicing with Ranges</h2> 1312<p>Ranges can also be used to extract slices of arrays. With two dots (<code>3..6</code>), the range is inclusive (<code>3, 4, 5, 6</code>); with three dots (<code>3...6</code>), the range excludes the end (<code>3, 4, 5</code>). Slices indices have useful defaults. An omitted first index defaults to zero and an omitted second index defaults to the size of the array.</p> 1313<div class='code'><pre><code>numbers = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>] 1314 1315start = numbers[<span class="number">0.</span><span class="number">.2</span>] 1316 1317middle = numbers[<span class="number">3.</span>..<span class="number">-2</span>] 1318 1319end = numbers[<span class="number">-2.</span>.] 1320 1321copy = numbers[..] 1322</code></pre><pre><code><span class="keyword">var</span> copy, end, middle, numbers, start; 1323 1324numbers = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>]; 1325 1326start = numbers.slice(<span class="number">0</span>, <span class="number">3</span>); 1327 1328middle = numbers.slice(<span class="number">3</span>, <span class="number">-2</span>); 1329 1330end = numbers.slice(<span class="number">-2</span>); 1331 1332copy = numbers.slice(<span class="number">0</span>); 1333</code></pre>
1333<script>window.example1 = "numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n\nstart = numbers[0..2]\n\nmiddle = numbers[3...-2]\n\nend = numbers[-2..]\n\ncopy = numbers[..]\n"</script>
1333<div class='minibutton load' onclick='javascript: loadConsole(example1);'>load</div><div class="minibutton ok" onclick="javascript: var copy, end, middle, numbers, start; 1334 1335numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]; 1336 1337start = numbers.slice(0, 3); 1338 1339middle = numbers.slice(3, -2); 1340 1341end = numbers.slice(-2); 1342 1343copy = numbers.slice(0); 1344;alert(middle);">run: middle</div><br class='clear' /></div><p>The same syntax can be used with assignment to replace a segment of an array with new values, splicing it.</p> 1345<div class='code'><pre><code>numbers = [<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>] 1346 1347numbers[<span class="number">3.</span><span class="number">.6</span>] = [<span class="number">-3</span>, <span class="number">-4</span>, <span class="number">-5</span>, <span class="number">-6</span>] 1348</code></pre><pre><code><span class="keyword">var</span> numbers, ref; 1349 1350numbers = [<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>]; 1351 1352[].splice.apply(numbers, [<span class="number">3</span>, <span class="number">4</span>].concat(ref = [<span class="number">-3</span>, <span class="number">-4</span>, <span class="number">-5</span>, <span class="number">-6</span>])), ref; 1353</code></pre>
1353<script>window.example2 = "numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n\nnumbers[3..6] = [-3, -4, -5, -6]\n"</script>
1353<div class='minibutton load' onclick='javascript: loadConsole(example2);'>load</div><div class="minibutton ok" onclick="javascript: var numbers, ref; 1354 1355numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; 1356 1357[].splice.apply(numbers, [3, 4].concat(ref = [-3, -4, -5, -6])), ref; 1358;alert(numbers);">run: numbers</div><br class='clear' /></div><p>Note that JavaScript strings are immutable, and canât be spliced.</p> 1359 1360 <span class="bookmark" id="expressions"></span> 1361 <h2>Everything is an Expression (at least, as much as possible)</h2> 1362<p>You might have noticed how even though we donât add return statements to CoffeeScript functions, they nonetheless return their final value. The CoffeeScript compiler tries to make sure that all statements in the language can be used as expressions. Watch how the <code>return</code> gets pushed down into each possible branch of execution in the function below.</p> 1363<div class='code'><pre><code><span class="function"><span class="title">grade</span> = <span class="params">(student)</span> -></span> 1364 <span class="keyword">if</span> student.excellentWork 1365 <span class="string">"A+"</span> 1366 <span class="keyword">else</span> <span class="keyword">if</span> student.okayStuff 1367 <span class="keyword">if</span> student.triedHard <span class="keyword">then</span> <span class="string">"B"</span> <span class="keyword">else</span> <span class="string">"B-"</span> 1368 <span class="keyword">else</span> 1369 <span class="string">"C"</span> 1370 1371eldest = <span class="keyword">if</span> <span class="number">24</span> > <span class="number">21</span> <span class="keyword">then</span> <span class="string">"Liz"</span> <span class="keyword">else</span> <span class="string">"Ike"</span> 1372</code></pre><pre><code><span class="keyword">var</span> eldest, grade; 1373 1374grade = <span class="function"><span class="keyword">function</span>(<span class="params">student</span>
1374) </span>{ 1375 <span class="keyword">if</span> (student.excellentWork) { 1376 <span class="keyword">return</span> <span class="string">"A+"</span>; 1377 } <span class="keyword">else</span> <span class="keyword">if</span> (student.okayStuff) { 1378 <span class="keyword">if</span> (student.triedHard) { 1379 <span class="keyword">return</span> <span class="string">"B"</span>; 1380 } <span class="keyword">else</span> { 1381 <span class="keyword">return</span> <span class="string">"B-"</span>; 1382 } 1383 } <span class="keyword">else</span> { 1384 <span class="keyword">return</span> <span class="string">"C"</span>; 1385 } 1386}; 1387 1388eldest = <span class="number">24</span> > <span class="number">21</span> ? <span class="string">"Liz"</span> : <span class="string">"Ike"</span>; 1389</code></pre>
1389<script>window.example1 = "grade = (student) ->\n if student.excellentWork\n \"A+\"\n else if student.okayStuff\n if student.triedHard then \"B\" else \"B-\"\n else\n \"C\"\n\neldest = if 24 > 21 then \"Liz\" else \"Ike\"\n"</script>
1389<div class='minibutton load' onclick='javascript: loadConsole(example1);'>load</div><div class="minibutton ok" onclick="javascript: var eldest, grade; 1390 1391grade = function(student) { 1392 if (student.excellentWork) { 1393 return "A+"; 1394 } else if (student.okayStuff) { 1395 if (student.triedHard) { 1396 return "B"; 1397 } else { 1398 return "B-"; 1399 } 1400 } else { 1401 return "C"; 1402 } 1403}; 1404 1405eldest = 24 > 21 ? "Liz" : "Ike"; 1406;alert(eldest);">run: eldest</div><br class='clear' /></div><p>Even though functions will always return their final value, itâs both possible and encouraged to return early from a function body writing out the explicit return (<code>return value</code>), when you know that youâre done.</p> 1407<p>Because variable declarations occur at the top of scope, assignment can be used within expressions, even for variables that havenât been seen before:</p> 1408<div class='code'><pre><code>six = (one = <span class="number">1</span>) + (two = <span class="number">2</span>) + (three = <span class="number">3</span>) 1409</code></pre><pre><code><span class="keyword">var</span> one, six, three, two; 1410 1411six = (one = <span class="number">1</span>) + (two = <span class="number">2</span>) + (three = <span class="number">3</span>); 1412</code></pre>
1412<script>window.example2 = "six = (one = 1) + (two = 2) + (three = 3)\n"</script>
1412<div class='minibutton load' onclick='javascript: loadConsole(example2);'>load</div><div class="minibutton ok" onclick="javascript: var one, six, three, two; 1413 1414six = (one = 1) + (two = 2) + (three = 3); 1415;alert(six);">run: six</div><br class='clear' /></div><p>Things that would otherwise be statements in JavaScript, when used as part of an expression in CoffeeScript, are converted into expressions by wrapping them in a closure. This lets you do useful things, like assign the result of a comprehension to a variable:</p> 1416<div class='code'><pre><code><span class="comment"># The first ten global properties.</span> 1417 1418globals = (name <span class="keyword">for</span> name <span class="keyword">of</span> <span class="built_in">window</span>)[<span class="number">0.</span>.<span class="number">.10</span>] 1419</code></pre><pre><code><span class="keyword">var</span> globals, name; 1420 1421globals = ((<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>{ 1422 <span class="keyword">var</span> results; 1423 results = []; 1424 <span class="keyword">for</span> (name <span class="keyword">in</span> <span class="built_in">window</span>) { 1425 results.push(name); 1426 } 1427 <span class="keyword">return</span> results; 1428})()).slice(<span class="number">0</span>, <span class="number">10</span>); 1429</code></pre>
1429<script>window.example3 = "# The first ten global properties.\n\nglobals = (name for name of window)[0...10]\n"</script>
1429<div class='minibutton load' onclick='javascript: loadConsole(example3);'>load</div><div class="minibutton ok" onclick="javascript: var globals, name; 1430 1431globals = ((function() { 1432 var results; 1433 results = []; 1434 for (name in window) { 1435 results.push(name); 1436 } 1437 return results; 1438})()).slice(0, 10); 1439;alert(globals);">run: globals</div><br class='clear' /></div><p>As well as silly things, like passing a <strong>try/catch</strong> statement directly into a function call:</p> 1440<div class='code'><pre><code>alert( 1441 <span class="keyword">try</span> 1442 nonexistent / <span class="literal">undefined</span> 1443 <span class="keyword">catch</span> error 1444 <span class="string">"And the error is ... <span class="subst">#{error}</span>"</span> 1445) 1446</code></pre><pre><code><span class="keyword">var</span> error; 1447 1448alert((<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>{ 1449 <span class="keyword">try</span> { 1450 <span class="keyword">return</span> nonexistent / <span class="keyword">void</span> <span class="number">0</span>; 1451 } <span class="keyword">catch</span> (error1) { 1452 error = error1; 1453 <span class="keyword">return</span> <span class="string">"And the error is ... "</span> + error; 1454 } 1455})()); 1456</code></pre>
1456<script>window.example4 = "alert(\n try\n nonexistent / undefined\n catch error\n \"And the error is ... #{error}\"\n)\n"</script>
1456<div class='minibutton load' onclick='javascript: loadConsole(example4);'>load</div><div class="minibutton ok" onclick="javascript: var error; 1457 1458alert((function() { 1459 try { 1460 return nonexistent / void 0; 1461 } catch (error1) { 1462 error = error1; 1463 return "And the error is ... " + error; 1464 } 1465})()); 1466;">run</div><br class='clear' /></div><p>There are a handful of statements in JavaScript that canât be meaningfully converted into expressions, namely <code>break</code>, <code>continue</code>, and <code>return</code>. If you make use of them within a block of code, CoffeeScript wonât try to perform the conversion.</p> 1467 1468 <span class="bookmark" id="operators"></span> 1469 <h2>Operators and Aliases</h2> 1470<p>Because the <code>==</code> operator frequently causes undesirable coercion, is intransitive, and has a different meaning than in other languages, CoffeeScript compiles <code>==</code> into <code>===</code>, and <code>!=</code> into <code>!==</code>. In addition, <code>is</code> compiles into <code>===</code>, and <code>isnt</code> into <code>!==</code>.</p> 1471<p>You can use <code>not</code> as an alias for <code>!</code>.</p> 1472<p>For logic, <code>and</code> compiles to <code>&&</code>, and <code>or</code> into <code>||</code>.</p> 1473<p>Instead of a newline or semicolon, <code>then</code> can be used to separate conditions from expressions, in <strong>while</strong>, <strong>if</strong>/<strong>else</strong>, and <strong>switch</strong>/<strong>when</strong> statements.</p> 1474<p>As in <a href="http://yaml.org/">YAML</a>, <code>on</code> and <code>yes</code> are the same as boolean <code>true</code>, while <code>off</code> and <code>no</code> are boolean <code>false</code>.</p> 1475<p><code>unless</code> can be used as the inverse of <code>if</code>.</p> 1476<p>As a shortcut for <code>this.property</code>, you can use <code>@property</code>.</p> 1477<p>You can use <code>in</code> to test for array presence, and <code>of</code> to test for JavaScript object-key presence.</p> 1478<p>To simplify math expressions, <code>**</code> can be used for exponentiation and <code>//</code> performs integer division. <code>%</code> works just like in JavaScript, while <code>%%</code> provides <a href="https://en.wikipedia.org/wiki/Modulo_operation">âdividend dependent moduloâ</a>:</p> 1479<div class='code'><pre><code><span class="number">-7</span> % <span class="number">5</span> == <span class="number">-2</span> <span class="comment"># The remainder of 7 / 5</span> 1480<span class="number">-7</span> %% <span class="number">5</span> == <span class="number">3</span> <span class="comment"># n %% 5 is always between 0 and 4</span> 1481 1482tabs.selectTabAtIndex((tabs.currentIndex - count) %% tabs.length) 1483</code></pre><pre><code><span class="keyword">var</span> modulo = <span class="function"><span class="keyword">function</span>(<span class="params">a, b</span>
1483) </span>{ <span class="keyword">return</span> (+a % (b = +b) + b) % b; }; 1484 1485<span class="number">-7</span> % <span class="number">5</span> === <span class="number">-2</span>; 1486 1487modulo(<span class="number">-7</span>, <span class="number">5</span>) === <span class="number">3</span>; 1488 1489tabs.selectTabAtIndex(modulo(tabs.currentIndex - count, tabs.length)); 1490</code></pre>
1490<script>window.example1 = "-7 % 5 == -2 # The remainder of 7 / 5\n-7 %% 5 == 3 # n %% 5 is always between 0 and 4\n\ntabs.selectTabAtIndex((tabs.currentIndex - count) %% tabs.length)\n"</script>
1490<div class='minibutton load' onclick='javascript: loadConsole(example1);'>load</div><br class='clear' /></div><p>All together now:</p> 1491<table> 1492<thead> 1493<tr> 1494<th>CoffeeScript</th> 1495<th>JavaScript</th> 1496</tr> 1497</thead> 1498<tbody> 1499<tr> 1500<td><code>is</code></td> 1501<td><code>===</code></td> 1502</tr> 1503<tr> 1504<td><code>isnt</code></td> 1505<td><code>!==</code></td> 1506</tr> 1507<tr> 1508<td><code>not</code></td> 1509<td><code>!</code></td> 1510</tr> 1511<tr> 1512<td><code>and</code></td> 1513<td><code>&&</code></td> 1514</tr> 1515<tr> 1516<td><code>or</code></td> 1517<td><code>||</code></td> 1518</tr> 1519<tr> 1520<td><code>true</code>, <code>yes</code>, <code>on</code></td> 1521<td><code>true</code></td> 1522</tr> 1523<tr> 1524<td><code>false</code>, <code>no</code>, <code>off</code>â</td> 1525<td><code>false</code></td> 1526</tr> 1527<tr> 1528<td><code>@</code>, <code>this</code></td> 1529<td><code>this</code></td> 1530</tr> 1531<tr> 1532<td><code>of</code></td> 1533<td><code>in</code></td> 1534</tr> 1535<tr> 1536<td><code>in</code></td> 1537<td><em>no JS equivalent</em></td> 1538</tr> 1539<tr> 1540<td><code>a ** b</code></td> 1541<td><code>Math.pow(a, b)</code></td> 1542</tr> 1543<tr> 1544<td><code>a // b</code></td> 1545<td><code>Math.floor(a / b)</code></td> 1546</tr> 1547<tr> 1548<td><code>a %% b</code></td> 1549<td><code>(a % b + b) % b</code></td> 1550</tr> 1551</tbody> 1552</table> 1553<div class='code'><pre><code>launch() <span class="keyword">if</span> ignition <span class="keyword">is</span> <span class="literal">on</span> 1554 1555volume = <span class="number">10</span> <span class="keyword">if</span> band <span class="keyword">isnt</span> SpinalTap 1556 1557letTheWildRumpusBegin() <span class="keyword">unless</span> answer <span class="keyword">is</span> <span class="literal">no</span> 1558 1559<span class="keyword">if</span> car.speed < limit <span class="keyword">then</span> accelerate() 1560 1561winner = <span class="literal">yes</span> <span class="keyword">if</span> pick <span class="keyword">in</span> [<span class="number">47</span>, <span class="number">92</span>, <span class="number">13</span>] 1562 1563<span class="built_in">print</span> inspect <span class="string">"My name is <span class="subst">#{@name}</span>"</span> 1564</code></pre><pre><code><span class="keyword">var</span> volume, winner; 1565 1566<span class="keyword">if</span> (ignition === <span class="literal">true</span>) { 1567 launch(); 1568} 1569 1570<span class="keyword">if</span> (band !== SpinalTap) { 1571 volume = <span class="number">10</span>; 1572} 1573 1574<span class="keyword">if</span> (answer !== <span class="literal">false</span>) { 1575 letTheWildRumpusBegin(); 1576} 1577 1578<span class="keyword">if</span> (car.speed < limit) { 1579 accelerate(); 1580} 1581 1582<span class="keyword">if</span> (pick === <span class="number">47</span> || pick === <span class="number">92</span> || pick === <span class="number">13</span>) { 1583 winner = <span class="literal">true</span>; 1584} 1585 1586print(inspect(<span class="string">"My name is "</span> + <span class="keyword">this</span>.name)); 1587</code></pre>
1587<script>window.example2 = "launch() if ignition is on\n\nvolume = 10 if band isnt SpinalTap\n\nletTheWildRumpusBegin() unless answer is no\n\nif car.speed < limit then accelerate()\n\nwinner = yes if pick in [47, 92, 13]\n\nprint inspect \"My name is #{@name}\"\n"</script>
1587<div class='minibutton load' onclick='javascript: loadConsole(example2);'>load</div><br class='clear' /></div> 1588 <span class="bookmark" id="existential-operator"></span> 1589 <h2>The Existential Operator</h2> 1590<p>Itâs a little difficult to check for the existence of a variable in JavaScript. <code>if (variable) â¦</code> comes close, but fails for zero, the empty string, and false. CoffeeScriptâs existential operator <code>?</code> returns true unless a variable is <strong>null</strong> or <strong>undefined</strong>, which makes it analogous to Rubyâs <code>nil?</code></p> 1591<p>It can also be used for safer conditional assignment than <code>||=</code> provides, for cases where you may be handling numbers or strings.</p> 1592<div class='code'><pre><code>solipsism = <span class="literal">true</span> <span class="keyword">if</span> mind? <span class="keyword">and</span> <span class="keyword">not</span> world? 1593 1594speed = <span class="number">0</span> 1595speed ?= <span class="number">15</span> 1596 1597footprints = yeti ? <span class="string">"bear"</span> 1598</code></pre><pre><code><span class="keyword">var</span> footprints, solipsism, speed; 1599 1600<span class="keyword">if</span> ((<span class="keyword">typeof</span> mind !== <span class="string">"undefined"</span> && mind !== <span class="literal">null</span>) && (<span class="keyword">typeof</span> world === <span class="string">"undefined"</span> || world === <span class="literal">null</span>)) { 1601 solipsism = <span class="literal">true</span>; 1602} 1603 1604speed = <span class="number">0</span>; 1605 1606<span class="keyword">if</span> (speed == <span class="literal">null</span>) { 1607 speed = <span class="number">15</span>; 1608} 1609 1610footprints = <span class="keyword">typeof</span> yeti !== <span class="string">"undefined"</span> && yeti !== <span class="literal">null</span> ? yeti : <span class="string">"bear"</span>; 1611</code></pre>
1611<script>window.example1 = "solipsism = true if mind? and not world?\n\nspeed = 0\nspeed ?= 15\n\nfootprints = yeti ? \"bear\"\n"</script>
1611<div class='minibutton load' onclick='javascript: loadConsole(example1);'>load</div><div class="minibutton ok" onclick="javascript: var footprints, solipsism, speed; 1612 1613if ((typeof mind !== "undefined" && mind !== null) && (typeof world === "undefined" || world === null)) { 1614 solipsism = true; 1615} 1616 1617speed = 0; 1618 1619if (speed == null) { 1620 speed = 15; 1621} 1622 1623footprints = typeof yeti !== "undefined" && yeti !== null ? yeti : "bear"; 1624;alert(footprints);">run: footprints</div><br class='clear' /></div><p>The accessor variant of the existential operator <code>?.</code> can be used to soak up null references in a chain of properties. Use it instead of the dot accessor <code>.</code> in cases where the base value may be <strong>null</strong> or <strong>undefined</strong>. If all of the properties exist then youâll get the expected result, if the chain is broken, <strong>undefined</strong> is returned instead of the <strong>TypeError</strong> that would be raised otherwise.</p> 1625<div class='code'><pre><code>zip = lottery.drawWinner?().address?.zipcode 1626</code></pre><pre><code><span class="keyword">var</span> ref, zip; 1627 1628zip = <span class="keyword">typeof</span> lottery.drawWinner === <span class="string">"function"</span> ? (ref = lottery.drawWinner().address) != <span class="literal">null</span> ? ref.zipcode : <span class="keyword">void</span> <span class="number">0</span> : <span class="keyword">void</span> <span class="number">0</span>; 1629</code></pre>
1629<script>window.example2 = "zip = lottery.drawWinner?().address?.zipcode\n"</script>
1629<div class='minibutton load' onclick='javascript: loadConsole(example2);'>load</div><br class='clear' /></div><p>Soaking up nulls is similar to Rubyâs <a href="https://rubygems.org/gems/andand">andand gem</a>, and to the <a href="http://docs.groovy-lang.org/latest/html/documentation/index.html#_safe_navigation_operator">safe navigation operator</a> in Groovy.</p> 1630 1631 <span class="bookmark" id="classes"></span> 1632 <h2>Classes, Inheritance, and Super</h2> 1633<p>JavaScriptâs prototypal inheritance has always been a bit of a brain-bender, with a whole family tree of libraries that provide a cleaner syntax for classical inheritance on top of JavaScriptâs prototypes: <a href="https://code.google.com/p/base2/">Base2</a>, <a href="http://prototypejs.org/">Prototype.js</a>, <a href="http://jsclass.jcoglan.com/">JS.Class</a>, etc. The libraries provide syntactic sugar, but the built-in inheritance would be completely usable if it werenât for a couple of small exceptions: itâs awkward to call <strong>super</strong> (the prototype objectâs implementation of the current function), and itâs awkward to correctly set the prototype chain.</p> 1634<p>Instead of repetitively attaching functions to a prototype, CoffeeScript provides a basic <code>class</code> structure that allows you to name your class, set the superclass, assign prototypal properties, and define the constructor, in a single assignable expression.</p> 1635<p>Constructor functions are named, to better support helpful stack traces. In the first class in the example below, <code>this.constructor.name is "Animal"</code>.</p> 1636<div class='code'><pre><code><span class="class"><span class="keyword">class</span> <span class="title">Animal</span></span> 1637 constructor: <span class="function"><span class="params">(@name)</span> -></span> 1638 1639 move: <span class="function"><span class="params">(meters)</span> -></span> 1640 alert @name + <span class="string">" moved <span class="subst">#{meters}</span>m."</span> 1641 1642<span class="class"><span class="keyword">class</span> <span class="title">Snake</span> <span class="keyword">extends</span> <span class="title">Animal</span></span> 1643 move: <span class="function">-></span> 1644 alert <span class="string">"Slithering..."</span> 1645 <span class="keyword">super</span> <span class="number">5</span> 1646 1647<span class="class"><span class="keyword">class</span> <span class="title">Horse</span> <span class="keyword">extends</span> <span class="title">Animal</span></span> 1648 move: <span class="function">-></span> 1649 alert <span class="string">"Galloping..."</span> 1650 <span class="keyword">super</span> <span class="number">45</span> 1651 1652sam = <span class="keyword">new</span> Snake <span class="string">"Sammy the Python"</span> 1653tom = <span class="keyword">new</span> Horse <span class="string">"Tommy the Palomino"</span> 1654 1655sam.move() 1656tom.move() 1657</code></pre><pre><code><span class="keyword">var</span> Animal, Horse, Snake, sam, tom, 1658 extend = <span class="function"><span class="keyword">function</span>(<span class="params">child, parent</span>
1658) </span>{ <span class="keyword">for</span> (<span class="keyword">var</span> key <span class="keyword">in</span> parent) { <span class="keyword">if</span> (hasProp.call(parent, key)) child[key] = parent[key]; } <span class="function"><span class="keyword">function</span> <span class="title">ctor</span>(<span class="params"></span>) </span>{ <span class="keyword">this</span>.constructor = child; } ctor.prototype = parent.prototype; child.prototype = <span class="keyword">new</span> ctor(); child.__super__ = parent.prototype; <span class="keyword">return</span> child; }, 1659 hasProp = {}.hasOwnProperty; 1660 1661Animal = (<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>{ 1662 <span class="function"><span class="keyword">function</span> <span class="title">Animal</span>(<span class="params">name</span>) </span>{ 1663 <span class="keyword">this</span>.name = name; 1664 } 1665 1666 Animal.prototype.move = <span class="function"><span class="keyword">function</span>(<span class="params">meters</span>
1666) </span>{ 1667 <span class="keyword">return</span> alert(<span class="keyword">this</span>.name + (<span class="string">" moved "</span> + meters + <span class="string">"m."</span>)); 1668 }; 1669 1670 <span class="keyword">return</span> Animal; 1671 1672})(); 1673 1674Snake = (<span class="function"><span class="keyword">function</span>(<span class="params">superClass</span>) </span>{ 1675 extend(Snake, superClass); 1676 1677 <span class="function"><span class="keyword">function</span> <span class="title">Snake</span>(<span class="params"></span>) </span>{ 1678 <span class="keyword">return</span> Snake.__super__.constructor.apply(<span class="keyword">this</span>, <span class="built_in">arguments</span>); 1679 } 1680 1681 Snake.prototype.move = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>{ 1682 alert(<span class="string">"Slithering..."</span>); 1683 <span class="keyword">return</span> Snake.__super__.move.call(<span class="keyword">this</span>, <span class="number">5</span>); 1684 }; 1685 1686 <span class="keyword">return</span> Snake; 1687 1688})(Animal); 1689 1690Horse = (<span class="function"><span class="keyword">function</span>(<span class="params">superClass</span>
1690) </span>{ 1691 extend(Horse, superClass); 1692 1693 <span class="function"><span class="keyword">function</span> <span class="title">Horse</span>(<span class="params"></span>) </span>{ 1694 <span class="keyword">return</span> Horse.__super__.constructor.apply(<span class="keyword">this</span>, <span class="built_in">arguments</span>); 1695 } 1696 1697 Horse.prototype.move = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>{ 1698 alert(<span class="string">"Galloping..."</span>); 1699 <span class="keyword">return</span> Horse.__super__.move.call(<span class="keyword">this</span>, <span class="number">45</span>); 1700 }; 1701 1702 <span class="keyword">return</span> Horse; 1703 1704})(Animal); 1705 1706sam = <span class="keyword">new</span> Snake(<span class="string">"Sammy the Python"</span>); 1707 1708tom = <span class="keyword">new</span> Horse(<span class="string">"Tommy the Palomino"</span>); 1709 1710sam.move(); 1711 1712tom.move(); 1713</code></pre>
1713<script>window.example1 = "class Animal\n constructor: (@name) ->\n\n move: (meters) ->\n alert @name + \" moved #{meters}m.\"\n\nclass Snake extends Animal\n move: ->\n alert \"Slithering...\"\n super 5\n\nclass Horse extends Animal\n move: ->\n alert \"Galloping...\"\n super 45\n\nsam = new Snake \"Sammy the Python\"\ntom = new Horse \"Tommy the Palomino\"\n\nsam.move()\ntom.move()\n"</script>
1713<div class='minibutton load' onclick='javascript: loadConsole(example1);'>load</div><div class="minibutton ok" onclick="javascript: var Animal, Horse, Snake, sam, tom, 1714 extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, 1715 hasProp = {}.hasOwnProperty; 1716 1717Animal = (function() { 1718 function Animal(name) { 1719 this.name = name; 1720 } 1721 1722 Animal.prototype.move = function(meters) { 1723 return alert(this.name + (" moved " + meters + "m.")); 1724 }; 1725 1726 return Animal; 1727 1728})(); 1729 1730Snake = (function(superClass) { 1731 extend(Snake, superClass); 1732 1733 function Snake() { 1734 return Snake.__super__.constructor.apply(this, arguments); 1735 } 1736 1737 Snake.prototype.move = function() { 1738 alert("Slithering..."); 1739 return Snake.__super__.move.call(this, 5); 1740 }; 1741 1742 return Snake; 1743 1744})(Animal); 1745 1746Horse = (function(superClass) { 1747 extend(Horse, superClass); 1748 1749 function Horse() { 1750 return Horse.__super__.constructor.apply(this, arguments); 1751 } 1752 1753 Horse.prototype.move = function() { 1754 alert("Galloping..."); 1755 return Horse.__super__.move.call(this, 45); 1756 }; 1757 1758 return Horse; 1759 1760})(Animal); 1761 1762sam = new Snake("Sammy the Python"); 1763 1764tom = new Horse("Tommy the Palomino"); 1765 1766sam.move(); 1767 1768tom.move(); 1769;">run</div><br class='clear' /></div><p>If structuring your prototypes classically isnât your cup of tea, CoffeeScript provides a couple of lower-level conveniences. The <code>extends</code> operator helps with proper prototype setup, and can be used to create an inheritance chain between any pair of constructor functions; <code>::</code> gives you quick access to an objectâs prototype; and <code>super()</code> is converted into a call against the immediate ancestorâs method of the same name.</p> 1770<div class='code'><pre><code>String::dasherize = <span class="function">-></span> 1771 <span class="keyword">this</span>.replace <span class="regexp">/_/g</span>, <span class="string">"-"</span> 1772</code></pre><pre><code><span class="built_in">String</span>.prototype.dasherize = <span class="function"><span class="keyword">function</span>(<span class="params"></span>
1772) </span>{ 1773 <span class="keyword">return</span> <span class="keyword">this</span>.replace(<span class="regexp">/_/g</span>, <span class="string">"-"</span>); 1774}; 1775</code></pre>
1775<script>window.example2 = "String::dasherize = ->\n this.replace /_/g, \"-\"\n"</script>
1775<div class='minibutton load' onclick='javascript: loadConsole(example2);'>load</div><div class="minibutton ok" onclick="javascript: String.prototype.dasherize = function() { 1776 return this.replace(/_/g, "-"); 1777}; 1778;alert("one_two".dasherize());">run: "one_two".dasherize()</div><br class='clear' /></div><p>Finally, class definitions are blocks of executable code, which make for interesting metaprogramming possibilities. Because in the context of a class definition, <code>this</code> is the class object itself (the constructor function), you can assign static properties by using 1779<code>@property: value</code>, and call functions defined in parent classes: <code>@attr 'title', type: 'text'</code></p> 1780 1781 <span class="bookmark" id="destructuring"></span> 1782 <h2>Destructuring Assignment</h2> 1783<p>Just like JavaScript (since ES2015), CoffeeScript has destructuring assignment syntax. When you assign an array or object literal to a value, CoffeeScript breaks up and matches both sides against each other, assigning the values on the right to the variables on the left. In the simplest case, it can be used for parallel assignment:</p> 1784<div class='code'><pre><code>theBait = <span class="number">1000</span> 1785theSwitch = <span class="number">0</span> 1786 1787[theBait, theSwitch] = [theSwitch, theBait] 1788</code></pre><pre><code><span class="keyword">var</span> ref, theBait, theSwitch; 1789 1790theBait = <span class="number">1000</span>; 1791 1792theSwitch = <span class="number">0</span>; 1793 1794ref = [theSwitch, theBait], theBait = ref[<span class="number">0</span>], theSwitch = ref[<span class="number">1</span>]; 1795</code></pre>
1795<script>window.example1 = "theBait = 1000\ntheSwitch = 0\n\n[theBait, theSwitch] = [theSwitch, theBait]\n"</script>
1795<div class='minibutton load' onclick='javascript: loadConsole(example1);'>load</div><div class="minibutton ok" onclick="javascript: var ref, theBait, theSwitch; 1796 1797theBait = 1000; 1798 1799theSwitch = 0; 1800 1801ref = [theSwitch, theBait], theBait = ref[0], theSwitch = ref[1]; 1802;alert(theBait);">run: theBait</div><br class='clear' /></div><p>But itâs also helpful for dealing with functions that return multiple values.</p> 1803<div class='code'><pre><code><span class="function"><span class="title">weatherReport</span> = <span class="params">(location)</span> -></span> 1804 <span class="comment"># Make an Ajax request to fetch the weather...</span> 1805 [location, <span class="number">72</span>, <span class="string">"Mostly Sunny"</span>] 1806 1807[city, temp, forecast] = weatherReport <span class="string">"Berkeley, CA"</span> 1808</code></pre><pre><code><span class="keyword">var</span> city, forecast, ref, temp, weatherReport; 1809 1810weatherReport = <span class="function"><span class="keyword">function</span>(<span class="params">location</span>) </span>{ 1811 <span class="keyword">return</span> [location, <span class="number">72</span>, <span class="string">"Mostly Sunny"</span>]; 1812}; 1813 1814ref = weatherReport(<span class="string">"Berkeley, CA"</span>), city = ref[<span class="number">0</span>], temp = ref[<span class="number">1</span>], forecast = ref[<span class="number">2</span>]; 1815</code></pre>
1815<script>window.example2 = "weatherReport = (location) ->\n # Make an Ajax request to fetch the weather...\n [location, 72, \"Mostly Sunny\"]\n\n[city, temp, forecast] = weatherReport \"Berkeley, CA\"\n"</script>
1815<div class='minibutton load' onclick='javascript: loadConsole(example2);'>load</div><div class="minibutton ok" onclick="javascript: var city, forecast, ref, temp, weatherReport; 1816 1817weatherReport = function(location) { 1818 return [location, 72, "Mostly Sunny"]; 1819}; 1820 1821ref = weatherReport("Berkeley, CA"), city = ref[0], temp = ref[1], forecast = ref[2]; 1822;alert(forecast);">run: forecast</div><br class='clear' /></div><p>Destructuring assignment can be used with any depth of array and object nesting, to help pull out deeply nested properties.</p> 1823<div class='code'><pre><code>futurists = 1824 sculptor: <span class="string">"Umberto Boccioni"</span> 1825 painter: <span class="string">"Vladimir Burliuk"</span> 1826 poet: 1827 name: <span class="string">"F.T. Marinetti"</span> 1828 address: [ 1829 <span class="string">"Via Roma 42R"</span> 1830 <span class="string">"Bellagio, Italy 22021"</span> 1831 ] 1832 1833{sculptor} = futurists 1834 1835{poet: {name, address: [street, city]}} = futurists 1836</code></pre><pre><code><span class="keyword">var</span> city, futurists, name, ref, ref1, sculptor, street; 1837 1838futurists = { 1839 <span class="attr">sculptor</span>: <span class="string">"Umberto Boccioni"</span>, 1840 <span class="attr">painter</span>: <span class="string">"Vladimir Burliuk"</span>, 1841 <span class="attr">poet</span>: { 1842 <span class="attr">name</span>: <span class="string">"F.T. Marinetti"</span>, 1843 <span class="attr">address</span>: [<span class="string">"Via Roma 42R"</span>, <span class="string">"Bellagio, Italy 22021"</span>] 1844 } 1845}; 1846 1847sculptor = futurists.sculptor; 1848 1849ref = futurists.poet, name = ref.name, (ref1 = ref.address, street = ref1[<span class="number">0</span>], city = ref1[<span class="number">1</span>]); 1850</code></pre>
1850<script>window.example3 = "futurists =\n sculptor: \"Umberto Boccioni\"\n painter: \"Vladimir Burliuk\"\n poet:\n name: \"F.T. Marinetti\"\n address: [\n \"Via Roma 42R\"\n \"Bellagio, Italy 22021\"\n ]\n\n{sculptor} = futurists\n\n{poet: {name, address: [street, city]}} = futurists\n"</script>
1850<div class='minibutton load' onclick='javascript: loadConsole(example3);'>load</div><div class="minibutton ok" onclick="javascript: var city, futurists, name, ref, ref1, sculptor, street; 1851 1852futurists = { 1853 sculptor: "Umberto Boccioni", 1854 painter: "Vladimir Burliuk", 1855 poet: { 1856 name: "F.T. Marinetti", 1857 address: ["Via Roma 42R", "Bellagio, Italy 22021"] 1858 } 1859}; 1860 1861sculptor = futurists.sculptor; 1862 1863ref = futurists.poet, name = ref.name, (ref1 = ref.address, street = ref1[0], city = ref1[1]); 1864;alert(name + "-" + street);">run: name + "-" + street</div><br class='clear' /></div><p>Destructuring assignment can even be combined with splats.</p> 1865<div class='code'><pre><code>tag = <span class="string">"<impossible>"</span> 1866 1867[open, contents..., close] = tag.split(<span class="string">""</span>) 1868</code></pre><pre><code><span class="keyword">var</span> close, contents, i, open, ref, tag, 1869 slice = [].slice; 1870 1871tag = <span class="string">"<impossible>"</span>; 1872 1873ref = tag.split(<span class="string">""</span>), open = ref[<span class="number">0</span>], contents = <span class="number">3</span> <= ref.length ? slice.call(ref, <span class="number">1</span>, i = ref.length - <span class="number">1</span>) : (i = <span class="number">1</span>, []), close = ref[i++]; 1874</code></pre>
1874<script>window.example4 = "tag = \"<impossible>\"\n\n[open, contents..., close] = tag.split(\"\")\n"</script>
1874<div class='minibutton load' onclick='javascript: loadConsole(example4);'>load</div><div class="minibutton ok" onclick="javascript: var close, contents, i, open, ref, tag, 1875 slice = [].slice; 1876 1877tag = "<impossible>"; 1878 1879ref = tag.split(""), open = ref[0], contents = 3 <= ref.length ? slice.call(ref, 1, i = ref.length - 1) : (i = 1, []), close = ref[i++]; 1880;alert(contents.join(""));">run: contents.join("")</div><br class='clear' /></div><p>Expansion can be used to retrieve elements from the end of an array without having to assign the rest of its values. It works in function parameter lists as well.</p> 1881<div class='code'><pre><code>text = <span class="string">"Every literary critic believes he will 1882 outwit history and have the last word"</span> 1883 1884[first, ..., last] = text.split <span class="string">" "</span> 1885</code></pre><pre><code><span class="keyword">var</span> first, last, ref, text; 1886 1887text = <span class="string">"Every literary critic believes he will outwit history and have the last word"</span>; 1888 1889ref = text.split(<span class="string">" "</span>), first = ref[<span class="number">0</span>], last = ref[ref.length - <span class="number">1</span>]; 1890</code></pre>
1890<script>window.example5 = "text = \"Every literary critic believes he will\n outwit history and have the last word\"\n\n[first, ..., last] = text.split \" \"\n"</script>
1890<div class='minibutton load' onclick='javascript: loadConsole(example5);'>load</div><div class="minibutton ok" onclick="javascript: var first, last, ref, text; 1891 1892text = "Every literary critic believes he will outwit history and have the last word"; 1893 1894ref = text.split(" "), first = ref[0], last = ref[ref.length - 1]; 1895;alert(first + " " + last);">run: first + " " + last</div><br class='clear' /></div><p>Destructuring assignment is also useful when combined with class constructors to assign properties to your instance from an options object passed to the constructor.</p> 1896<div class='code'><pre><code><span class="class"><span class="keyword">class</span> <span class="title">Person</span></span> 1897 constructor: <span class="function"><span class="params">(options)</span> -></span> 1898 {@name, @age, @height = <span class="string">'average'</span>} = options 1899 1900tim = <span class="keyword">new</span> Person name: <span class="string">'Tim'</span>, age: <span class="number">4</span> 1901</code></pre><pre><code><span class="keyword">var</span> Person, tim; 1902 1903Person = (<span class="function"><span class="keyword">function</span>(<span class="params"></span>
1903) </span>{ 1904 <span class="function"><span class="keyword">function</span> <span class="title">Person</span>(<span class="params">options</span>) </span>{ 1905 <span class="keyword">var</span> ref; 1906 <span class="keyword">this</span>.name = options.name, <span class="keyword">this</span>.age = options.age, <span class="keyword">this</span>.height = (ref = options.height) != <span class="literal">null</span> ? ref : <span class="string">'average'</span>; 1907 } 1908 1909 <span class="keyword">return</span> Person; 1910 1911})(); 1912 1913tim = <span class="keyword">new</span> Person({ 1914 <span class="attr">name</span>: <span class="string">'Tim'</span>, 1915 <span class="attr">age</span>: <span class="number">4</span> 1916}); 1917</code></pre>
1917<script>window.example6 = "class Person\n constructor: (options) ->\n {@name, @age, @height = 'average'} = options\n\ntim = new Person name: 'Tim', age: 4\n"</script>
1917<div class='minibutton load' onclick='javascript: loadConsole(example6);'>load</div><div class="minibutton ok" onclick="javascript: var Person, tim; 1918 1919Person = (function() { 1920 function Person(options) { 1921 var ref; 1922 this.name = options.name, this.age = options.age, this.height = (ref = options.height) != null ? ref : 'average'; 1923 } 1924 1925 return Person; 1926 1927})(); 1928 1929tim = new Person({ 1930 name: 'Tim', 1931 age: 4 1932}); 1933;alert(tim.age + " " + tim.height);">run: tim.age + " " + tim.height</div><br class='clear' /></div><p>The above example also demonstrates that if properties are missing in the destructured object or array, you can, just like in JavaScript, provide defaults. The difference with JavaScript is that CoffeeScript, as always, treats both null and undefined the same.</p> 1934 1935 <span class="bookmark" id="fat-arrow"></span> 1936 <h2>Bound Functions, Generator Functions</h2> 1937<p>In JavaScript, the <code>this</code> keyword is dynamically scoped to mean the object that the current function is attached to. If you pass a function as a callback or attach it to a different object, the original value of <code>this</code> will be lost. If youâre not familiar with this behavior, <a href="http://64.13.255.16/articles/scope_in_javascript/">this Digital Web article</a> gives a good overview of the quirks.</p> 1938<p>The fat arrow <code>=></code> can be used to both define a function, and to bind it to the current value of <code>this</code>, right on the spot. This is helpful when using callback-based libraries like Prototype or jQuery, for creating iterator functions to pass to <code>each</code>, or event-handler functions to use with <code>on</code>. Functions created with the fat arrow are able to access properties of the <code>this</code> where theyâre defined.</p> 1939<div class='code'><pre><code><span class="function"><span class="title">Account</span> = <span class="params">(customer, cart)</span> -></span> 1940 @customer = customer 1941 @cart = cart 1942 1943 $(<span class="string">'.shopping_cart'</span>).<span class="literal">on</span> <span class="string">'click'</span>, <span class="function"><span class="params">(event)</span> =></span> 1944 @customer.purchase @cart 1945</code></pre><pre><code><span class="keyword">var</span> Account; 1946 1947Account = <span class="function"><span class="keyword">function</span>(<span class="params">customer, cart</span>
1947) </span>{ 1948 <span class="keyword">this</span>.customer = customer; 1949 <span class="keyword">this</span>.cart = cart; 1950 <span class="keyword">return</span> $(<span class="string">'.shopping_cart'</span>).on(<span class="string">'click'</span>, (<span class="function"><span class="keyword">function</span>(<span class="params">_this</span>) </span>{ 1951 <span class="keyword">return</span> <span class="function"><span class="keyword">function</span>(<span class="params">event</span>) </span>{ 1952 <span class="keyword">return</span> _this.customer.purchase(_this.cart); 1953 }; 1954 })(<span class="keyword">this</span>)); 1955}; 1956</code></pre>
1956<script>window.example1 = "Account = (customer, cart) ->\n @customer = customer\n @cart = cart\n\n $('.shopping_cart').on 'click', (event) =>\n @customer.purchase @cart\n"</script>
1956<div class='minibutton load' onclick='javascript: loadConsole(example1);'>load</div><br class='clear' /></div><p>If we had used <code>-></code> in the callback above, <code>@customer</code> would have referred to the undefined âcustomerâ property of the DOM element, and trying to call <code>purchase()</code> on it would have raised an exception.</p> 1957<p>When used in a class definition, methods declared with the fat arrow will be automatically bound to each instance of the class when the instance is constructed.</p> 1958<div id="generator-functions" class="bookmark"></div> 1959<p>CoffeeScript functions also support <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*">ES2015 generator functions</a> through the <code>yield</code> keyword. Thereâs no <code>function*(){}</code> nonsense â a generator in CoffeeScript is simply a function that yields.</p> 1960<div class='code'><pre><code><span class="function"><span class="title">perfectSquares</span> = -></span> 1961 num = <span class="number">0</span> 1962 <span class="keyword">loop</span> 1963 num += <span class="number">1</span> 1964 <span class="keyword">yield</span> num * num 1965 <span class="keyword">return</span> 1966 1967<span class="built_in">window</span>.ps <span class="keyword">or</span>= perfectSquares() 1968</code></pre><pre><code><span class="keyword">var</span> perfectSquares; 1969 1970perfectSquares = <span class="function"><span class="keyword">function</span>*(<span class="params"></span>
1970) </span>{ 1971 <span class="keyword">var</span> num; 1972 num = <span class="number">0</span>; 1973 <span class="keyword">while</span> (<span class="literal">true</span>) { 1974 num += <span class="number">1</span>; 1975 <span class="keyword">yield</span> num * num; 1976 } 1977}; 1978 1979<span class="built_in">window</span>.ps || (<span class="built_in">window</span>.ps = perfectSquares()); 1980</code></pre>
1980<script>window.example2 = "perfectSquares = ->\n num = 0\n loop\n num += 1\n yield num * num\n return\n\nwindow.ps or= perfectSquares()\n"</script>
1980<div class='minibutton load' onclick='javascript: loadConsole(example2);'>load</div><div class="minibutton ok" onclick="javascript: var perfectSquares; 1981 1982perfectSquares = function*() { 1983 var num; 1984 num = 0; 1985 while (true) { 1986 num += 1; 1987 yield num * num; 1988 } 1989}; 1990 1991window.ps || (window.ps = perfectSquares()); 1992;alert(ps.next().value);">run: ps.next().value</div><br class='clear' /></div><p><code>yield*</code> is called <code>yield from</code>, and <code>yield return</code> may be used if you need to force a generator that doesnât yield.</p> 1993<div id="generator-iteration" class="bookmark"></div> 1994<p>You can iterate over a generator function using <code>forâ¦from</code>.</p> 1995<div class='code'><pre><code><span class="function"><span class="title">fibonacci</span> = -></span> 1996 [previous, current] = [<span class="number">1</span>, <span class="number">1</span>] 1997 <span class="keyword">loop</span> 1998 [previous, current] = [current, previous + current] 1999 <span class="keyword">yield</span> current 2000 <span class="keyword">return</span> 2001<span class="function"> 2002<span class="title">getFibonacciNumbers</span> = <span class="params">(length)</span> -></span> 2003 results = [<span class="number">1</span>] 2004 <span class="keyword">for</span> n <span class="keyword">from</span> fibonacci() 2005 results.push n 2006 <span class="keyword">break</span> <span class="keyword">if</span> results.length <span class="keyword">is</span> length 2007 results 2008</code></pre><pre><code><span class="keyword">var</span> fibonacci, getFibonacciNumbers; 2009 2010fibonacci = <span class="function"><span class="keyword">function</span>*(<span class="params"></span>
2010) </span>{ 2011 <span class="keyword">var</span> current, previous, ref, ref1; 2012 ref = [<span class="number">1</span>, <span class="number">1</span>], previous = ref[<span class="number">0</span>], current = ref[<span class="number">1</span>]; 2013 <span class="keyword">while</span> (<span class="literal">true</span>) { 2014 ref1 = [current, previous + current], previous = ref1[<span class="number">0</span>], current = ref1[<span class="number">1</span>]; 2015 <span class="keyword">yield</span> current; 2016 } 2017}; 2018 2019getFibonacciNumbers = <span class="function"><span class="keyword">function</span>(<span class="params">length</span>) </span>{ 2020 <span class="keyword">var</span> n, ref, results; 2021 results = [<span class="number">1</span>]; 2022 ref = fibonacci(); 2023 <span class="keyword">for</span> (n <span class="keyword">of</span> ref) { 2024 results.push(n); 2025 <span class="keyword">if</span> (results.length === length) { 2026 <span class="keyword">break</span>; 2027 } 2028 } 2029 <span class="keyword">return</span> results; 2030}; 2031</code></pre>
2031<script>window.example3 = "fibonacci = ->\n [previous, current] = [1, 1]\n loop\n [previous, current] = [current, previous + current]\n yield current\n return\n\ngetFibonacciNumbers = (length) ->\n results = [1]\n for n from fibonacci()\n results.push n\n break if results.length is length\n results\n"</script>
2031<div class='minibutton load' onclick='javascript: loadConsole(example3);'>load</div><div class="minibutton ok" onclick="javascript: var fibonacci, getFibonacciNumbers; 2032 2033fibonacci = function*() { 2034 var current, previous, ref, ref1; 2035 ref = [1, 1], previous = ref[0], current = ref[1]; 2036 while (true) { 2037 ref1 = [current, previous + current], previous = ref1[0], current = ref1[1]; 2038 yield current; 2039 } 2040}; 2041 2042getFibonacciNumbers = function(length) { 2043 var n, ref, results; 2044 results = [1]; 2045 ref = fibonacci(); 2046 for (n of ref) { 2047 results.push(n); 2048 if (results.length === length) { 2049 break; 2050 } 2051 } 2052 return results; 2053}; 2054;alert(getFibonacciNumbers(10));">run: getFibonacciNumbers(10)</div><br class='clear' /></div> 2055 <span class="bookmark" id="embedded"></span> 2056 <h2>Embedded JavaScript</h2> 2057<p>Hopefully, youâll never need to use it, but if you ever need to intersperse snippets of JavaScript within your CoffeeScript, you can use backticks to pass it straight through.</p> 2058<div class='code'><pre><code>hi = `<span class="javascript"><span class="function"><span class="keyword">function</span>(<span class="params"></span>
2058) </span>{ 2059 <span class="keyword">return</span> [<span class="built_in">document</span>.title, <span class="string">"Hello JavaScript"</span>].join(<span class="string">": "</span>); 2060}</span>` 2061</code></pre><pre><code><span class="keyword">var</span> hi; 2062 2063hi = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>{ 2064 <span class="keyword">return</span> [<span class="built_in">document</span>.title, <span class="string">"Hello JavaScript"</span>].join(<span class="string">": "</span>); 2065}; 2066</code></pre>
2066<script>window.example1 = "hi = `function() {\n return [document.title, \"Hello JavaScript\"].join(\": \");\n}`\n"</script>
2066<div class='minibutton load' onclick='javascript: loadConsole(example1);'>load</div><div class="minibutton ok" onclick="javascript: var hi; 2067 2068hi = function() { 2069 return [document.title, "Hello JavaScript"].join(": "); 2070}; 2071;alert(hi());">run: hi()</div><br class='clear' /></div><p>Escape backticks with backslashes: <code>\`â</code> becomes <code>`â</code>.</p> 2072<p>Escape backslashes before backticks with more backslashes: <code>\\\`â</code> becomes <code>\`â</code>.</p> 2073<div class='code'><pre><code>markdown = `<span class="javascript"><span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{ 2074 <span class="keyword">return</span> \</span>`In Markdown, write code like \\\`<span class="javascript"><span class="keyword">this</span>\\\</span>`\`<span class="javascript">; 2075}</span>` 2076</code></pre><pre><code><span class="keyword">var</span> markdown; 2077 2078markdown = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{ 2079 <span class="keyword">return</span> <span class="string">`In Markdown, write code like \`this\``</span>; 2080}; 2081</code></pre>
2081<script>window.example2 = "markdown = `function () {\n return \\`In Markdown, write code like \\\\\\`this\\\\\\`\\`;\n}`\n"</script>
2081<div class='minibutton load' onclick='javascript: loadConsole(example2);'>load</div><div class="minibutton ok" onclick="javascript: var markdown; 2082 2083markdown = function () { 2084 return `In Markdown, write code like \`this\``; 2085}; 2086;alert(markdown());">run: markdown()</div><br class='clear' /></div><p>You can also embed blocks of JavaScript using triple backticks. Thatâs easier than escaping backticks, if you need them inside your JavaScript block.</p> 2087<div class='code'><pre><code>```<span class="javascript"> 2088<span class="function"><span class="keyword">function</span> <span class="title">time</span>(<span class="params"></span>) </span>{ 2089 <span class="keyword">return</span> <span class="string">`The time is <span class="subst">${<span class="keyword">new</span> <span class="built_in">Date</span>().toLocaleTimeString()}</span>`</span>; 2090} 2091</span>``` 2092</code></pre><pre><code> 2093<span class="function"><span class="keyword">function</span> <span class="title">time</span>(<span class="params"></span>) </span>{ 2094 <span class="keyword">return</span> <span class="string">`The time is <span class="subst">${<span class="keyword">new</span> <span class="built_in">Date</span>().toLocaleTimeString()}</span>`</span>; 2095} 2096; 2097 2098</code></pre>
2098<script>window.example3 = "```\nfunction time() {\n return `The time is ${new Date().toLocaleTimeString()}`;\n}\n```\n"</script>
2098<div class='minibutton load' onclick='javascript: loadConsole(example3);'>load</div><div class="minibutton ok" onclick="javascript: 2099function time() { 2100 return `The time is ${new Date().toLocaleTimeString()}`; 2101} 2102; 2103 2104;alert(time());">run: time()</div><br class='clear' /></div> 2105 <span class="bookmark" id="switch"></span> 2106 <h2>Switch/When/Else</h2> 2107<p><strong>Switch</strong> statements in JavaScript are a bit awkward. You need to remember to <strong>break</strong> at the end of every <strong>case</strong> statement to avoid accidentally falling through to the default case. CoffeeScript prevents accidental fall-through, and can convert the <code>switch</code> into a returnable, assignable expression. The format is: <code>switch</code> condition, <code>when</code> clauses, <code>else</code> the default case.</p> 2108<p>As in Ruby, <strong>switch</strong> statements in CoffeeScript can take multiple values for each <strong>when</strong> clause. If any of the values match, the clause runs.</p> 2109<div class='code'><pre><code><span class="keyword">switch</span> day 2110 <span class="keyword">when</span> <span class="string">"Mon"</span> <span class="keyword">then</span> go work 2111 <span class="keyword">when</span> <span class="string">"Tue"</span> <span class="keyword">then</span> go relax 2112 <span class="keyword">when</span> <span class="string">"Thu"</span> <span class="keyword">then</span> go iceFishing 2113 <span class="keyword">when</span> <span class="string">"Fri"</span>, <span class="string">"Sat"</span> 2114 <span class="keyword">if</span> day <span class="keyword">is</span> bingoDay 2115 go bingo 2116 go dancing 2117 <span class="keyword">when</span> <span class="string">"Sun"</span> <span class="keyword">then</span> go church 2118 <span class="keyword">else</span> go work 2119</code></pre><pre><code><span class="keyword">switch</span> (day) { 2120 <span class="keyword">case</span> <span class="string">"Mon"</span>: 2121 go(work); 2122 <span class="keyword">break</span>; 2123 <span class="keyword">case</span> <span class="string">"Tue"</span>: 2124 go(relax); 2125 <span class="keyword">break</span>; 2126 <span class="keyword">case</span> <span class="string">"Thu"</span>: 2127 go(iceFishing); 2128 <span class="keyword">break</span>; 2129 <span class="keyword">case</span> <span class="string">"Fri"</span>: 2130 <span class="keyword">case</span> <span class="string">"Sat"</span>: 2131 <span class="keyword">if</span> (day === bingoDay) { 2132 go(bingo); 2133 go(dancing); 2134 } 2135 <span class="keyword">break</span>; 2136 <span class="keyword">case</span> <span class="string">"Sun"</span>: 2137 go(church); 2138 <span class="keyword">break</span>; 2139 <span class="keyword">default</span>: 2140 go(work); 2141} 2142</code></pre>
2142<script>window.example1 = "switch day\n when \"Mon\" then go work\n when \"Tue\" then go relax\n when \"Thu\" then go iceFishing\n when \"Fri\", \"Sat\"\n if day is bingoDay\n go bingo\n go dancing\n when \"Sun\" then go church\n else go work\n"</script>
2142<div class='minibutton load' onclick='javascript: loadConsole(example1);'>load</div><br class='clear' /></div><p>Switch statements can also be used without a control expression, turning them in to a cleaner alternative to if/else chains.</p> 2143<div class='code'><pre><code>score = <span class="number">76</span> 2144grade = <span class="keyword">switch</span> 2145 <span class="keyword">when</span> score < <span class="number">60</span> <span class="keyword">then</span> <span class="string">'F'</span> 2146 <span class="keyword">when</span> score < <span class="number">70</span> <span class="keyword">then</span> <span class="string">'D'</span> 2147 <span class="keyword">when</span> score < <span class="number">80</span> <span class="keyword">then</span> <span class="string">'C'</span> 2148 <span class="keyword">when</span> score < <span class="number">90</span> <span class="keyword">then</span> <span class="string">'B'</span> 2149 <span class="keyword">else</span> <span class="string">'A'</span> 2150<span class="comment"># grade == 'C'</span> 2151</code></pre><pre><code><span class="keyword">var</span> grade, score; 2152 2153score = <span class="number">76</span>; 2154 2155grade = (<span class="function"><span class="keyword">function</span>(<span class="params"></span>
2155) </span>{ 2156 <span class="keyword">switch</span> (<span class="literal">false</span>) { 2157 <span class="keyword">case</span> !(score < <span class="number">60</span>): 2158 <span class="keyword">return</span> <span class="string">'F'</span>; 2159 <span class="keyword">case</span> !(score < <span class="number">70</span>): 2160 <span class="keyword">return</span> <span class="string">'D'</span>; 2161 <span class="keyword">case</span> !(score < <span class="number">80</span>): 2162 <span class="keyword">return</span> <span class="string">'C'</span>; 2163 <span class="keyword">case</span> !(score < <span class="number">90</span>): 2164 <span class="keyword">return</span> <span class="string">'B'</span>; 2165 <span class="keyword">default</span>: 2166 <span class="keyword">return</span> <span class="string">'A'</span>; 2167 } 2168})(); 2169</code></pre>
2169<script>window.example2 = "score = 76\ngrade = switch\n when score < 60 then 'F'\n when score < 70 then 'D'\n when score < 80 then 'C'\n when score < 90 then 'B'\n else 'A'\n# grade == 'C'\n"</script>
2169<div class='minibutton load' onclick='javascript: loadConsole(example2);'>load</div><br class='clear' /></div> 2170 <span class="bookmark" id="try-catch"></span> 2171 <h2>Try/Catch/Finally</h2> 2172<p>Try-expressions have the same semantics as try-statements in JavaScript, though in CoffeeScript, you may omit <em>both</em> the catch and finally parts. The catch part may also omit the error parameter if it is not needed.</p> 2173<div class='code'><pre><code><span class="keyword">try</span> 2174 allHellBreaksLoose() 2175 catsAndDogsLivingTogether() 2176<span class="keyword">catch</span> error 2177 <span class="built_in">print</span> error 2178<span class="keyword">finally</span> 2179 cleanUp() 2180</code></pre><pre><code><span class="keyword">var</span> error; 2181 2182<span class="keyword">try</span> { 2183 allHellBreaksLoose(); 2184 catsAndDogsLivingTogether(); 2185} <span class="keyword">catch</span> (error1) { 2186 error = error1; 2187 print(error); 2188} <span class="keyword">finally</span> { 2189 cleanUp(); 2190} 2191</code></pre>
2191<script>window.example1 = "try\n allHellBreaksLoose()\n catsAndDogsLivingTogether()\ncatch error\n print error\nfinally\n cleanUp()\n"</script>
2191<div class='minibutton load' onclick='javascript: loadConsole(example1);'>load</div><br class='clear' /></div> 2192 <span class="bookmark" id="comparisons"></span> 2193 <h2>Chained Comparisons</h2> 2194<p>CoffeeScript borrows <a href="https://docs.python.org/3/reference/expressions.html#not-in">chained comparisons</a> from Python â making it easy to test if a value falls within a certain range.</p> 2195<div class='code'><pre><code>cholesterol = <span class="number">127</span> 2196 2197healthy = <span class="number">200</span> > cholesterol > <span class="number">60</span> 2198</code></pre><pre><code><span class="keyword">var</span> cholesterol, healthy; 2199 2200cholesterol = <span class="number">127</span>; 2201 2202healthy = (<span class="number">200</span> > cholesterol && cholesterol > <span class="number">60</span>); 2203</code></pre>
2203<script>window.example1 = "cholesterol = 127\n\nhealthy = 200 > cholesterol > 60\n"</script>
2203<div class='minibutton load' onclick='javascript: loadConsole(example1);'>load</div><div class="minibutton ok" onclick="javascript: var cholesterol, healthy; 2204 2205cholesterol = 127; 2206 2207healthy = (200 > cholesterol && cholesterol > 60); 2208;alert(healthy);">run: healthy</div><br class='clear' /></div> 2209 <span class="bookmark" id="strings"></span> 2210 <h2>String Interpolation, Block Strings, and Block Comments</h2> 2211<p>Ruby-style string interpolation is included in CoffeeScript. Double-quoted strings allow for interpolated values, using <code>#{ ⦠}</code>, and single-quoted strings are literal. You may even use interpolation in object keys.</p> 2212<div class='code'><pre><code>author = <span class="string">"Wittgenstein"</span> 2213quote = <span class="string">"A picture is a fact. -- <span class="subst">#{ author }</span>"</span> 2214 2215sentence = <span class="string">"<span class="subst">#{ <span class="number">22</span> / <span class="number">7</span> }</span> is a decent approximation of Ï"</span> 2216</code></pre><pre><code><span class="keyword">var</span> author, quote, sentence; 2217 2218author = <span class="string">"Wittgenstein"</span>; 2219 2220quote = <span class="string">"A picture is a fact. -- "</span> + author; 2221 2222sentence = (<span class="number">22</span> / <span class="number">7</span>) + <span class="string">" is a decent approximation of Ï"</span>; 2223</code></pre>
2223<script>window.example1 = "author = \"Wittgenstein\"\nquote = \"A picture is a fact. -- #{ author }\"\n\nsentence = \"#{ 22 / 7 } is a decent approximation of Ï\"\n"</script>
2223<div class='minibutton load' onclick='javascript: loadConsole(example1);'>load</div><div class="minibutton ok" onclick="javascript: var author, quote, sentence; 2224 2225author = "Wittgenstein"; 2226 2227quote = "A picture is a fact. -- " + author; 2228 2229sentence = (22 / 7) + " is a decent approximation of Ï"; 2230;alert(sentence);">run: sentence</div><br class='clear' /></div><p>Multiline strings are allowed in CoffeeScript. Lines are joined by a single space unless they end with a backslash. Indentation is ignored.</p> 2231<div class='code'><pre><code>mobyDick = <span class="string">"Call me Ishmael. Some years ago -- 2232 never mind how long precisely -- having little 2233 or no money in my purse, and nothing particular 2234 to interest me on shore, I thought I would sail 2235 about a little and see the watery part of the 2236 world..."</span> 2237</code></pre><pre><code><span class="keyword">var</span> mobyDick; 2238 2239mobyDick = <span class="string">"Call me Ishmael. Some years ago -- never mind how long precisely -- having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world..."</span>; 2240</code></pre>
2240<script>window.example2 = "mobyDick = \"Call me Ishmael. Some years ago --\n never mind how long precisely -- having little\n or no money in my purse, and nothing particular\n to interest me on shore, I thought I would sail\n about a little and see the watery part of the\n world...\"\n"</script>
2240<div class='minibutton load' onclick='javascript: loadConsole(example2);'>load</div><div class="minibutton ok" onclick="javascript: var mobyDick; 2241 2242mobyDick = "Call me Ishmael. Some years ago -- never mind how long precisely -- having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world..."; 2243;alert(mobyDick);">run: mobyDick</div><br class='clear' /></div><p>Block strings can be used to hold formatted or indentation-sensitive text (or, if you just donât feel like escaping quotes and apostrophes). The indentation level that begins the block is maintained throughout, so you can keep it all aligned with the body of your code.</p> 2244<div class='code'><pre><code>html = <span class="string">""" 2245 <strong> 2246 cup of coffeescript 2247 </strong> 2248 """</span> 2249</code></pre><pre><code><span class="keyword">var</span> html; 2250 2251html = <span class="string">"<strong>\n cup of coffeescript\n</strong>"</span>; 2252</code></pre>
2252<script>window.example3 = "html = \"\"\"\n <strong>\n cup of coffeescript\n </strong>\n \"\"\"\n"</script>
2252<div class='minibutton load' onclick='javascript: loadConsole(example3);'>load</div><div class="minibutton ok" onclick="javascript: var html; 2253 2254html = "<strong>\n cup of coffeescript\n</strong>"; 2255;alert(html);">run: html</div><br class='clear' /></div><p>Double-quoted block strings, like other double-quoted strings, allow interpolation.</p> 2256<p>Sometimes youâd like to pass a block comment through to the generated JavaScript. For example, when you need to embed a licensing header at the top of a file. Block comments, which mirror the syntax for block strings, are preserved in the generated code.</p> 2257<div class='code'><pre><code><span class="comment">### 2258SkinnyMochaHalfCaffScript Compiler v1.0 2259Released under the MIT License 2260###</span> 2261</code></pre><pre><code> 2262<span class="comment">/* 2263SkinnyMochaHalfCaffScript Compiler v1.0 2264Released under the MIT License 2265 */</span> 2266 2267</code></pre>
2267<script>window.example4 = "###\nSkinnyMochaHalfCaffScript Compiler v1.0\nReleased under the MIT License\n###\n"</script>
2267<div class='minibutton load' onclick='javascript: loadConsole(example4);'>load</div><br class='clear' /></div> 2268 <span class="bookmark" id="tagged-template-literals"></span> 2269 <h2>Tagged Template Literals</h2> 2270<p>CoffeeScript supports <a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Template_literals#Tagged_template_literals">ES2015 tagged template literals</a>, which enable customized string interpolation. If you immediately prefix a string with a function name (no space between the two), CoffeeScript will output this âfunction plus stringâ combination as an ES2015 tagged template literal, which will <a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Template_literals#Tagged_template_literals">behave accordingly</a>: the function is called, with the parameters being the input text and expression parts that make up the interpolated string. The function can then assemble these parts into an output string, providing custom string interpolation.</p> 2271<p>Be aware that the CoffeeScript compiler is outputting ES2015 syntax for this feature, so your target JavaScript runtime(s) must support this syntax for your code to work; or you could use tools like <a href="http://babeljs.io/">Babel</a> or <a href="https://github.com/google/traceur-compiler">Traceur Compiler</a> to convert this ES2015 syntax into compatible JavaScript.</p> 2272<div class='code'><pre><code><span class="function"><span class="title">upperCaseExpr</span> = <span class="params">(textParts, expressions...)</span> -></span> 2273 textParts.reduce (text, textPart, i) -> 2274 text + expressions[i - <span class="number">1</span>].toUpperCase() + textPart 2275<span class="function"> 2276<span class="title">greet</span> = <span class="params">(name, adjective)</span> -></span> 2277 upperCaseExpr<span class="string">""" 2278 Hi <span class="subst">#{name}</span>. You look <span class="subst">#{adjective}</span>! 2279 """</span> 2280</code></pre><pre><code><span class="keyword">var</span> greet, upperCaseExpr, 2281 slice = [].slice; 2282 2283upperCaseExpr = <span class="function"><span class="keyword">function</span>(<span class="params"></span>
2283) </span>{ 2284 <span class="keyword">var</span> expressions, textParts; 2285 textParts = <span class="built_in">arguments</span>[<span class="number">0</span>], expressions = <span class="number">2</span> <= <span class="built_in">arguments</span>.length ? slice.call(<span class="built_in">arguments</span>, <span class="number">1</span>) : []; 2286 <span class="keyword">return</span> textParts.reduce(<span class="function"><span class="keyword">function</span>(<span class="params">text, textPart, i</span>) </span>{ 2287 <span class="keyword">return</span> text + expressions[i - <span class="number">1</span>].toUpperCase() + textPart; 2288 }); 2289}; 2290 2291greet = <span class="function"><span class="keyword">function</span>(<span class="params">name, adjective</span>) </span>{ 2292 <span class="keyword">return</span> upperCaseExpr<span class="string">`Hi <span class="subst">${name}</span>. You look <span class="subst">${adjective}</span>!`</span>; 2293}; 2294</code></pre>
2294<script>window.example1 = "upperCaseExpr = (textParts, expressions...) ->\n textParts.reduce (text, textPart, i) ->\n text + expressions[i - 1].toUpperCase() + textPart\n\ngreet = (name, adjective) ->\n upperCaseExpr\"\"\"\n Hi #{name}. You look #{adjective}!\n \"\"\"\n"</script>
2294<div class='minibutton load' onclick='javascript: loadConsole(example1);'>load</div><div class="minibutton ok" onclick="javascript: var greet, upperCaseExpr, 2295 slice = [].slice; 2296 2297upperCaseExpr = function() { 2298 var expressions, textParts; 2299 textParts = arguments[0], expressions = 2 <= arguments.length ? slice.call(arguments, 1) : []; 2300 return textParts.reduce(function(text, textPart, i) { 2301 return text + expressions[i - 1].toUpperCase() + textPart; 2302 }); 2303}; 2304 2305greet = function(name, adjective) { 2306 return upperCaseExpr`Hi ${name}. You look ${adjective}!`; 2307}; 2308;alert(greet("greg", "awesome"));">run: greet("greg", "awesome")</div><br class='clear' /></div> 2309 <span class="bookmark" id="regexes"></span> 2310 <h2>Block Regular Expressions</h2> 2311<p>Similar to block strings and comments, CoffeeScript supports block regexes â extended regular expressions that ignore internal whitespace and can contain comments and interpolation. Modeled after Perlâs <code>/x</code> modifier, CoffeeScriptâs block regexes are delimited by <code>///</code> and go a long way towards making complex regular expressions readable. To quote from the CoffeeScript source:</p> 2312<div class='code'><pre><code>OPERATOR = <span class="regexp">/// ^ ( 2313 ?: [-=]> <span class="comment"># function</span> 2314 | [-+*/%<>&|^!?=]= <span class="comment"># compound assign / compare</span> 2315 | >>>=? <span class="comment"># zero-fill right shift</span> 2316 | ([-+:])\1 <span class="comment"># doubles</span> 2317 | ([&|<>])\2=? <span class="comment"># logic / shift</span> 2318 | \?\. <span class="comment"># soak access</span> 2319 | \.{2,3} <span class="comment"># range or splat</span> 2320) ///</span> 2321</code></pre><pre><code><span class="keyword">var</span> OPERATOR; 2322 2323OPERATOR = <span class="regexp">/^(?:[-=]>|[-+*\/%<>&|^!?=]=|>>>=?|([-+:])\1|([&|<>])\2=?|\?\.|\.{2,3})/</span>; 2324</code></pre>
2324<script>window.example1 = "OPERATOR = /// ^ (\n ?: [-=]> # function\n | [-+*/%<>&|^!?=]= # compound assign / compare\n | >>>=? # zero-fill right shift\n | ([-+:])\\1 # doubles\n | ([&|<>])\\2=? # logic / shift\n | \\?\\. # soak access\n | \\.{2,3} # range or splat\n) ///\n"</script>
2324<div class='minibutton load' onclick='javascript: loadConsole(example1);'>load</div><br class='clear' /></div> 2325 <span class="bookmark" id="modules"></span> 2326 <h2>Modules</h2> 2327<p>ES2015 modules are supported in CoffeeScript, with very similar <code>import</code> and <code>export</code> syntax:</p> 2328<div class='code'><pre><code><span class="keyword">import</span> <span class="string">'local-file.coffee'</span> 2329<span class="keyword">import</span> <span class="string">'coffeescript'</span> 2330 2331<span class="keyword">import</span> _ <span class="keyword">from</span> <span class="string">'underscore'</span> 2332<span class="keyword">import</span> * <span class="keyword">as</span> underscore <span class="keyword">from</span> <span class="string">'underscore'</span> 2333 2334<span class="keyword">import</span> { now } <span class="keyword">from</span> <span class="string">'underscore'</span> 2335<span class="keyword">import</span> { now <span class="keyword">as</span> currentTimestamp } <span class="keyword">from</span> <span class="string">'underscore'</span> 2336<span class="keyword">import</span> { first, last } <span class="keyword">from</span> <span class="string">'underscore'</span> 2337<span class="keyword">import</span> utilityBelt, { each } <span class="keyword">from</span> <span class="string">'underscore'</span> 2338 2339<span class="keyword">export</span> <span class="keyword">default</span> Math 2340<span class="keyword">export</span> square = <span class="function"><span class="params">(x)</span> -></span> x * x 2341<span class="keyword">export</span> <span class="class"><span class="keyword">class</span> <span class="title">Mathematics</span></span> 2342 least: <span class="function"><span class="params">(x, y)</span> -></span> <span class="keyword">if</span> x < y <span class="keyword">then</span> x <span class="keyword">else</span> y 2343 2344<span class="keyword">export</span> { sqrt } 2345<span class="keyword">export</span> { sqrt <span class="keyword">as</span> squareRoot } 2346<span class="keyword">export</span> { Mathematics <span class="keyword">as</span> <span class="keyword">default</span>, sqrt <span class="keyword">as</span> squareRoot } 2347 2348<span class="keyword">export</span> * <span class="keyword">from</span> <span class="string">'underscore'</span> 2349<span class="keyword">export</span> { max, min } <span class="keyword">from</span> <span class="string">'underscore'</span> 2350</code></pre><pre><code><span class="keyword">import</span> <span class="string">'local-file.coffee'</span>; 2351 2352<span class="keyword">import</span> <span class="string">'coffeescript'</span>; 2353 2354<span class="keyword">import</span> _ <span class="keyword">from</span> <span class="string">'underscore'</span>; 2355 2356<span class="keyword">import</span> * <span class="keyword">as</span> underscore <span class="keyword">from</span> <span class="string">'underscore'</span>; 2357 2358<span class="keyword">import</span> { 2359 now 2360} <span class="keyword">from</span> <span class="string">'underscore'</span>; 2361 2362<span class="keyword">import</span> { 2363 now <span class="keyword">as</span> currentTimestamp 2364} <span class="keyword">from</span> <span class="string">'underscore'</span>; 2365 2366<span class="keyword">import</span> { 2367 first, 2368 last 2369} <span class="keyword">from</span> <span class="string">'underscore'</span>; 2370 2371<span class="keyword">import</span> utilityBelt, { 2372 each 2373} <span class="keyword">from</span> <span class="string">'underscore'</span>; 2374 2375<span class="keyword">export</span> <span class="keyword">default</span> <span class="built_in">Math</span>; 2376 2377<span class="keyword">export</span> <span class="keyword">var</span> square = <span class="function"><span class="keyword">function</span>(<span class="params">x</span>
2377) </span>{ 2378 <span class="keyword">return</span> x * x; 2379}; 2380 2381<span class="keyword">export</span> <span class="keyword">var</span> Mathematics = (<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>{ 2382 <span class="function"><span class="keyword">function</span> <span class="title">Mathematics</span>(<span class="params"></span>) </span>{} 2383 2384 Mathematics.prototype.least = <span class="function"><span class="keyword">function</span>(<span class="params">x, y</span>) </span>{ 2385 <span class="keyword">if</span> (x < y) { 2386 <span class="keyword">return</span> x; 2387 } <span class="keyword">else</span> { 2388 <span class="keyword">return</span> y; 2389 } 2390 }; 2391 2392 <span class="keyword">return</span> Mathematics; 2393 2394})(); 2395 2396<span class="keyword">export</span> { 2397 sqrt 2398}; 2399 2400<span class="keyword">export</span> { 2401 sqrt <span class="keyword">as</span> squareRoot 2402}; 2403 2404<span class="keyword">export</span> { 2405 Mathematics <span class="keyword">as</span> <span class="keyword">default</span>, 2406 sqrt <span class="keyword">as</span> squareRoot 2407}; 2408 2409<span class="keyword">export</span> * <span class="keyword">from</span> <span class="string">'underscore'</span>; 2410 2411<span class="keyword">export</span> { 2412 max, 2413 min 2414} <span class="keyword">from</span> <span class="string">'underscore'</span>; 2415</code></pre>
2415<script>window.example1 = "import 'local-file.coffee'\nimport 'coffeescript'\n\nimport _ from 'underscore'\nimport * as underscore from 'underscore'\n\nimport { now } from 'underscore'\nimport { now as currentTimestamp } from 'underscore'\nimport { first, last } from 'underscore'\nimport utilityBelt, { each } from 'underscore'\n\nexport default Math\nexport square = (x) -> x * x\nexport class Mathematics\n least: (x, y) -> if x < y then x else y\n\nexport { sqrt }\nexport { sqrt as squareRoot }\nexport { Mathematics as default, sqrt as squareRoot }\n\nexport * from 'underscore'\nexport { max, min } from 'underscore'\n"</script>
2415<div class='minibutton load' onclick='javascript: loadConsole(example1);'>load</div><br class='clear' /></div><div id="modules-note" class="bookmark"></div> 2416<p>Note that the CoffeeScript compiler <strong>does not resolve modules</strong>; writing an <code>import</code> or <code>export</code> statement in CoffeeScript will produce an <code>import</code> or <code>export</code> statement in the resulting output. It is your responsibility attach another transpiler, such as <a href="https://github.com/google/traceur-compiler">Traceur Compiler</a>, <a href="http://babeljs.io/">Babel</a>Â or <a href="https://github.com/rollup/rollup">Rollup</a>, to convert this ES2015 syntax into code that will work in your target runtimes.</p> 2417<p>Also note that any file with an <code>import</code> or <code>export</code> statement will be output without a <a href="#lexical-scope">top-level function safety wrapper</a>; in other words, importing or exporting modules will automatically trigger <a href="#usage">bare</a> mode for that file. This is because per the ES2015 spec, <code>import</code> or <code>export</code> statements must occur at the topmost scope.</p> 2418 2419 <span class="bookmark" id="cake"></span> 2420 <h2>Cake, and Cakefiles</h2> 2421<p>CoffeeScript includes a (very) simple build system similar to <a href="http://www.gnu.org/software/make/">Make</a> and <a href="http://rake.rubyforge.org/">Rake</a>. Naturally, itâs called Cake, and is used for the tasks that build and test the CoffeeScript language itself. Tasks are defined in a file named <code>Cakefile</code>, and can be invoked by running <code>cake [task]</code> from within the directory. To print a list of all the tasks and options, just type <code>cake</code>.</p> 2422<p>Task definitions are written in CoffeeScript, so you can put arbitrary code in your Cakefile. Define a task with a name, a long description, and the function to invoke when the task is run. If your task takes a command-line option, you can define the option with short and long flags, and it will be made available in the <code>options</code> object. Hereâs a task that uses the Node.js API to rebuild CoffeeScriptâs parser:</p> 2423<div class='code'><pre><code>fs = <span class="built_in">require</span> <span class="string">'fs'</span> 2424 2425option <span class="string">'-o'</span>, <span class="string">'--output [DIR]'</span>, <span class="string">'directory for compiled code'</span> 2426 2427task <span class="string">'build:parser'</span>, <span class="string">'rebuild the Jison parser'</span>, <span class="function"><span class="params">(options)</span> -></span> 2428 <span class="built_in">require</span> <span class="string">'jison'</span> 2429 code = <span class="built_in">require</span>(<span class="string">'./lib/grammar'</span>).parser.generate() 2430 dir = options.output <span class="keyword">or</span> <span class="string">'lib'</span> 2431 fs.writeFile <span class="string">"<span class="subst">#{dir}</span>/parser.js"</span>, code 2432</code></pre><pre><code><span class="keyword">var</span> fs; 2433 2434fs = <span class="built_in">require</span>(<span class="string">'fs'</span>); 2435 2436option(<span class="string">'-o'</span>, <span class="string">'--output [DIR]'</span>, <span class="string">'directory for compiled code'</span>); 2437 2438task(<span class="string">'build:parser'</span>, <span class="string">'rebuild the Jison parser'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">options</span>
2438) </span>{ 2439 <span class="keyword">var</span> code, dir; 2440 <span class="built_in">require</span>(<span class="string">'jison'</span>); 2441 code = <span class="built_in">require</span>(<span class="string">'./lib/grammar'</span>).parser.generate(); 2442 dir = options.output || <span class="string">'lib'</span>; 2443 <span class="keyword">return</span> fs.writeFile(dir + <span class="string">"/parser.js"</span>, code); 2444}); 2445</code></pre>
2445<script>window.example1 = "fs = require 'fs'\n\noption '-o', '--output [DIR]', 'directory for compiled code'\n\ntask 'build:parser', 'rebuild the Jison parser', (options) ->\n require 'jison'\n code = require('./lib/grammar').parser.generate()\n dir = options.output or 'lib'\n fs.writeFile \"#{dir}/parser.js\", code\n"</script>
2445<div class='minibutton load' onclick='javascript: loadConsole(example1);'>load</div><br class='clear' /></div><p>If you need to invoke one task before another â for example, running <code>build</code> before <code>test</code>, you can use the <code>invoke</code> function: <code>invoke 'build'</code>. Cake tasks are a minimal way to expose your CoffeeScript functions to the command line, so <a href="annotated-source/cake.html">donât expect any fanciness built-in</a>. If you need dependencies, or async callbacks, itâs best to put them in your code itself â not the cake task.</p> 2446 2447 <span class="bookmark" id="source-maps"></span> 2448 <h2>Source Maps</h2> 2449<p>CoffeeScript 1.6.1 and above include support for generating source maps, a way to tell your JavaScript engine what part of your CoffeeScript program matches up with the code being evaluated. Browsers that support it can automatically use source maps to show your original source code in the debugger. To generate source maps alongside your JavaScript files, pass the <code>--map</code> or <code>-m</code> flag to the compiler.</p> 2450<p>For a full introduction to source maps, how they work, and how to hook them up in your browser, read the <a href="https://www.html5rocks.com/en/tutorials/developertools/sourcemaps/">HTML5 Tutorial</a>.</p> 2451 2452 <span class="bookmark" id="scripts"></span> 2453 <h2>âtext/coffeescriptâ Script Tags</h2> 2454<p>While itâs not recommended for serious use, CoffeeScripts may be included directly within the browser using <code><script type="text/coffeescript"></code> tags. The source includes a compressed and minified version of the compiler (<a href="browser-compiler/coffee-script.js">Download current version here, 51k when gzipped</a>) as <code>docs/v1/browser-compiler/coffee-script.js</code>. Include this file on a page with inline CoffeeScript tags, and it will compile and evaluate them in order.</p> 2455<p>In fact, the little bit of glue script that runs âTry CoffeeScriptâ above, as well as the jQuery for the menu, is implemented in just this way. View source and look at the bottom of the page to see the example. Including the script also gives you access to <code>CoffeeScript.compile()</code> so you can pop open Firebug and try compiling some strings.</p> 2456<p>The usual caveats about CoffeeScript apply â your inline scripts will run within a closure wrapper, so if you want to expose global variables or functions, attach them to the <code>window</code> object.</p> 2457 2458 <span class="bookmark" id="resources"></span> 2459 <h2>Books</h2> 2460<p>There are a number of excellent resources to help you get started with CoffeeScript, some of which are freely available online.</p> 2461<ul> 2462<li><a href="http://arcturo.github.io/library/coffeescript/">The Little Book on CoffeeScript</a> is a brief 5-chapter introduction to CoffeeScript, written with great clarity and precision by <a href="http://alexmaccaw.co.uk/">Alex MacCaw</a>.</li> 2463<li><a href="http://autotelicum.github.io/Smooth-CoffeeScript/">Smooth CoffeeScript</a> is a reimagination of the excellent book <a href="http://eloquentjavascript.net/">Eloquent JavaScript</a>, as if it had been written in CoffeeScript instead. Covers language features as well as the functional and object oriented programming styles. By <a href="https://github.com/autotelicum">E. Hoigaard</a>.</li> 2464<li><a href="http://pragprog.com/book/tbcoffee/coffeescript">CoffeeScript: Accelerated JavaScript Development</a> is <a href="http://trevorburnham.com/">Trevor Burnham</a>âs thorough introduction to the language. By the end of the book, youâll have built a fast-paced multiplayer word game, writing both the client-side and Node.js portions in CoffeeScript.</li> 2465<li><a href="https://www.packtpub.com/web-development/coffeescript-programming-jquery-rails-and-nodejs">CoffeeScript Programming with jQuery, Rails, and Node.js</a> is a new book by Michael Erasmus that covers CoffeeScript with an eye towards real-world usage both in the browser (jQuery) and on the server-side (Rails, Node).</li> 2466<li><a href="https://leanpub.com/coffeescript-ristretto/read">CoffeeScript Ristretto</a> is a deep dive into CoffeeScriptâs semantics from simple functions up through closures, higher-order functions, objects, classes, combinators, and decorators. By <a href="http://braythwayt.com/">Reg Braithwaite</a>.</li> 2467<li><a href="https://efendibooks.com/minibooks/testing-with-coffeescript">Testing with CoffeeScript</a> is a succinct and freely downloadable guide to building testable applications with CoffeeScript and Jasmine.</li> 2468<li><a href="https://www.packtpub.com/web-development/coffeescript-application-development">CoffeeScript Application Development</a> from Packt, introduces CoffeeScript while walking through the process of building a demonstration web application. A <a href="https://www.packtpub.com/web-development/coffeescript-application-development-cookbook">CoffeeScript Application Development Coookbook</a> with over 90 ârecipesâ is also available.</li> 2469<li><a href="https://www.manning.com/books/coffeescript-in-action">CoffeeScript in Action</a> from Manning Publications, covers CoffeeScript syntax, composition techniques and application development.</li> 2470<li><a href="https://www.dpunkt.de/buecher/4021/coffeescript.html">CoffeeScript: Die Alternative zu JavaScript</a> from dpunkt.verlag, is the first CoffeeScript book in Deutsch.</li> 2471</ul> 2472 2473 <span class="bookmark" id="screencasts"></span> 2474 <h2>Screencasts</h2> 2475<ul> 2476<li><a href="http://coffeescript.codeschool.com/">A Sip of CoffeeScript</a> is a <a href="https://www.codeschool.com">Code School Course</a> which combines 6 screencasts with in-browser coding to make learning fun. The first level is free to try out.</li> 2477<li><a href="https://www.pluralsight.com/courses/meet-coffeescript">Meet CoffeeScript</a> is a 75-minute long screencast by PeepCode, now <a href="https://www.pluralsight.com/">PluralSight</a>. Highly memorable for its animations which demonstrate transforming CoffeeScript into the equivalent JS.</li> 2478<li>If youâre looking for less of a time commitment, RailsCastsâ <a href="http://railscasts.com/episodes/267-coffeescript-basics">CoffeeScript Basics</a> should have you covered, hitting all of the important notes about CoffeeScript in 11 minutes.</li> 2479</ul> 2480 2481 <span class="bookmark" id="examples"></span> 2482 <h2>Examples</h2> 2483<p>The <a href="https://github.com/trending?l=coffeescript&since=monthly">best list of open-source CoffeeScript examples</a> can be found on GitHub. But just to throw out a few more:</p> 2484<ul> 2485<li><strong>GitHub</strong>âs <a href="https://hubot.github.com/">Hubot</a>, a friendly IRC robot that can perform any number of useful and useless tasks.</li> 2486<li><strong>sstephenson</strong>âs <a href="http://pow.cx/">Pow</a>, a zero-configuration Rack server, with comprehensive annotated source.</li> 2487<li><strong>technoweenie</strong>âs <a href="https://github.com/technoweenie/coffee-resque">Coffee-Resque</a>, a port of <a href="https://github.com/defunkt/resque">Resque</a> for Node.js.</li> 2488<li><strong>stephank</strong>âs <a href="https://github.com/stephank/orona">Orona</a>, a remake of the Bolo tank game for modern browsers.</li> 2489<li><strong>GitHub</strong>âs <a href="https://atom.io/">Atom</a>, a hackable text editor built on web technologies.</li> 2490<li><strong>Basecamp</strong>âs <a href="https://trix-editor.org/">Trix</a>, a rich text editor for web apps.</li> 2491</ul> 2492 2493 <span class="bookmark" id="additional-resources"></span> 2494 <h2>Resources</h2> 2495<ul> 2496<li> 2497<p><a href="https://github.com/jashkenas/coffeescript/">Source Code</a><br> 2498Use <code>bin/coffee</code> to test your changes,<br> 2499<code>bin/cake test</code> to run the test suite,<br> 2500<code>bin/cake build</code> to rebuild the full CoffeeScript compiler, and<br> 2501<code>bin/cake build:except-parser</code> to recompile much faster if youâre not editing <code>grammar.coffee</code>.</p> 2502<p><code>git checkout lib && bin/cake build:full</code> is a good command to run when youâre working on the core language. Itâll refresh the <code>lib</code> folder (in case you broke something), build your altered compiler, use that to rebuild itself (a good sanity test) and then run all of the tests. If they pass, thereâs a good chance youâve made a successful change.</p> 2503</li> 2504<li> 2505<p><a href="test.html">Browser Tests</a><br>
2506Run CoffeeScriptâs test suite in your current browser.</p> 2507</li> 2508<li> 2509<p><a href="https://github.com/jashkenas/coffeescript/issues">CoffeeScript Issues</a><br> 2510Bug reports, feature proposals, and ideas for changes to the language belong here.</p> 2511</li> 2512<li> 2513<p><a href="https://groups.google.com/forum/#!forum/coffeescript">CoffeeScript Google Group</a><br> 2514If youâd like to ask a question, the mailing list is a good place to get help.</p> 2515</li> 2516<li> 2517<p><a href="https://github.com/jashkenas/coffeescript/wiki">The CoffeeScript Wiki</a><br> 2518If youâve ever learned a neat CoffeeScript tip or trick, or ran into a gotcha â share it on the wiki. The wiki also serves as a directory of handy <a href="https://github.com/jashkenas/coffeescript/wiki/Text-editor-plugins">text editor extensions</a>, <a href="https://github.com/jashkenas/coffeescript/wiki/Web-framework-plugins">web framework plugins</a>, and general <a href="https://github.com/jashkenas/coffeescript/wiki/Build-tools">CoffeeScript build tools</a>.</p> 2519</li> 2520<li> 2521<p><a href="https://github.com/jashkenas/coffeescript/wiki/FAQ">The FAQ</a><br> 2522Perhaps your CoffeeScript-related question has been asked before. Check the FAQ first.</p> 2523</li> 2524<li> 2525<p><a href="http://js2coffee.org">JS2Coffee</a><br> 2526Is a very well done reverse JavaScript-to-CoffeeScript compiler. Itâs not going to be perfect (infer what your JavaScript classes are, when you need bound functions, and so onâ¦) â but itâs a great starting point for converting simple scripts.</p> 2527</li> 2528<li> 2529<p><a href="https://github.com/jashkenas/coffeescript/tree/master/documentation/images">High-Rez Logo</a><br> 2530The CoffeeScript logo is available in SVG for use in presentations.</p> 2531</li> 2532</ul> 2533 2534 <span class="bookmark" id="chat"></span> 2535 <h2>Web Chat (IRC)</h2> 2536<p>Quick help and advice can usually be found in the CoffeeScript IRC room. Join <code>#coffeescript</code> on <code>irc.freenode.net</code>, or click the button below to open a webchat session on this page.</p> 2537<p><button id="open_webchat">click to open #coffeescript</button></p> 2538 2539 <span class="bookmark" id="changelog"></span> 2540 <h2>Change Log</h2> 2541<div class="anchor" id="1.12.7"></div> 2542<h2 class="header"> 2543 <a href="https://github.com/jashkenas/coffeescript/compare/1.12.6...1.12.7">1.12.7</a> 2544 <span class="timestamp"> — <time datetime="2017-07-16">July 16, 2017</time></span> 2545</h2><ul> 2546<li>Fix regressions in 1.12.6 related to chained function calls and indented <code>return</code> and <code>throw</code> arguments.</li> 2547<li>The REPL no longer warns about assigning to <code>_</code>.</li> 2548</ul> 2549<div class="anchor" id="1.12.6"></div> 2550<h2 class="header"> 2551 <a href="https://github.com/jashkenas/coffeescript/compare/1.12.5...1.12.6">1.12.6</a> 2552 <span class="timestamp"> — <time datetime="2017-05-15">May 15, 2017</time></span> 2553</h2><ul> 2554<li>The <code>return</code> and <code>export</code> keywords can now accept implicit objects (defined by indentation, without needing braces).</li> 2555<li>Support Unicode code point escapes (e.g. <code>\u{1F4A9}</code>).</li> 2556<li>The <code>coffee</code> command now first looks to see if CoffeeScript is installed under <code>node_modules</code> in the current folder, and executes the <code>coffee</code> binary there if so; or otherwise it runs the globally installed one. This allows you to have one version of CoffeeScript installed globally and a different one installed locally for a particular project. (Likewise for the <code>cake</code> command.)</li> 2557<li>Bugfixes for chained function calls not closing implicit objects or ternaries.</li> 2558<li>Bugfixes for incorrect code generated by the <code>?</code> operator within a termary <code>if</code> statement.</li> 2559<li>Fixed some tests, and failing tests now result in a nonzero exit code.</li> 2560</ul> 2561<div class="anchor" id="1.12.5"></div> 2562<h2 class="header"> 2563 <a href="https://github.com/jashkenas/coffeescript/compare/1.12.4...1.12.5">1.12.5</a> 2564 <span class="timestamp"> — <time datetime="2017-04-10">April 10, 2017</time></span> 2565</h2><ul> 2566<li>Better handling of <code>default</code>, <code>from</code>, <code>as</code> and <code>*</code> within <code>import</code> and <code>export</code> statements. You can now import or export a member named <code>default</code> and the compiler wonât interpret it as the <code>default</code> keyword.</li> 2567<li>Fixed a bug where invalid octal escape sequences werenât throwing errors in the compiler.</li> 2568</ul> 2569<div class="anchor" id="1.12.4"></div> 2570<h2 class="header"> 2571 <a href="https://github.com/jashkenas/coffeescript/compare/1.12.3...1.12.4">1.12.4</a> 2572 <span class="timestamp"> — <time datetime="2017-02-18">February 18, 2017</time></span> 2573</h2><ul> 2574<li>The <code>cake</code> commands have been updated, with new <code>watch</code> options for most tasks. Clone the <a href="https://github.com/jashkenas/coffeescript">CoffeeScript repo</a> and run <code>cake</code>
2574 at the root of the repo to see the options.</li> 2575<li>Fixed a bug where <code>export</code>ing a referenced variable was preventing the variable from being declared.</li> 2576<li>Fixed a bug where the <code>coffee</code> command wasnât working for a <code>.litcoffee</code> file.</li> 2577<li>Bugfixes related to tokens and location data, for better source maps and improved compatibility with downstream tools.</li> 2578</ul> 2579<div class="anchor" id="1.12.3"></div> 2580<h2 class="header"> 2581 <a href="https://github.com/jashkenas/coffeescript/compare/1.12.2...1.12.3">1.12.3</a> 2582 <span class="timestamp"> — <time datetime="2017-01-24">January 24, 2017</time></span> 2583</h2><ul> 2584<li><code>@</code> values can now be used as indices in <code>for</code> expressions. This loosens the compilation of <code>for</code> expressions to allow the index variable to be an <code>@</code> value, e.g. <code>do @visit for @node, @index in nodes</code>. Within <code>@visit</code>, the index of the current node (<code>@node</code>) would be available as <code>@index</code>.</li> 2585<li>CoffeeScriptâs patched <code>Error.prepareStackTrace</code> has been restored, with some revisions that should prevent the erroneous exceptions that were making life difficult for some downstream projects. This fixes the incorrect line numbers in stack traces since 1.12.2.</li> 2586<li>The <code>//=</code> operatorâs output now wraps parentheses around the right operand, like the other assignment operators.</li> 2587</ul> 2588<div class="anchor" id="1.12.2"></div> 2589<h2 class="header"> 2590 <a href="https://github.com/jashkenas/coffeescript/compare/1.12.1...1.12.2">1.12.2</a> 2591 <span class="timestamp"> — <time datetime="2016-12-16">December 16, 2016</time></span> 2592</h2><ul> 2593<li>The browser compiler can once again be built unminified via <code>MINIFY=false cake build:browser</code>.</li> 2594<li>The error-prone patched version of <code>Error.prepareStackTrace</code> has been removed.</li> 2595<li>Command completion in the REPL (pressing tab to get suggestions) has been fixed for Node 6.9.1+.</li> 2596<li>The <a href="test.html">browser-based tests</a> now include all the tests as the Node-based version.</li> 2597</ul> 2598<div class="anchor" id="1.12.1"></div> 2599<h2 class="header"> 2600 <a href="https://github.com/jashkenas/coffeescript/compare/1.12.0...1.12.1">1.12.1</a> 2601 <span class="timestamp"> — <time datetime="2016-12-07">December 7, 2016</time></span> 2602</h2><ul> 2603<li>You can now import a module member named <code>default</code>, e.g. <code>import { default } from 'lib'</code>. Though like in ES2015, you cannot import an entire module and name it <code>default</code> (so <code>import default from 'lib'</code> is not allowed).</li> 2604<li>Fix regression where <code>from</code> as a variable name was breaking <code>for</code> loop declarations. For the record, <code>from</code> is not a reserved word in CoffeeScript; you may use it for variable names. <code>from</code> behaves like a keyword within the context of <code>import</code> and <code>export</code> statements, and in the declaration of a <code>for</code> loop; though you should also be able to use variables named <code>from</code> in those contexts, and the compiler should be able to tell the difference.</li> 2605</ul> 2606<div class="anchor" id="1.12.0"></div> 2607<h2 class="header"> 2608 <a href="https://github.com/jashkenas/coffeescript/compare/1.11.1...1.12.0">1.12.0</a> 2609 <span class="timestamp"> — <time datetime="2016-12-04">December 4, 2016</time></span> 2610</h2><ul> 2611<li>CoffeeScript now supports ES2015 <a href="#tagged-template-literals">tagged template literals</a>. Note that using tagged template literals in your code makes you responsible for ensuring that either your runtime supports tagged template literals or that you transpile the output JavaScript further to a version your target runtime(s) support.</li> 2612<li>CoffeeScript now provides a <a href="#generator-iteration"><code>forâ¦from</code></a> syntax for outputting ES2015 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of"><code>forâ¦of</code></a>. (Sorry they couldnât match, but we came up with <code>forâ¦of</code> first for something else.) This allows iterating over generators or any other iterable object. Note that using <code>forâ¦from</code> in your code makes you responsible for ensuring that either your runtime supports <code>forâ¦of</code> or that you transpile the output JavaScript further to a version your target runtime(s) support.</li> 2613<li>Triple backticks (<code>```â</code>) allow the creation of embedded JavaScript blocks where escaping single backticks is not required, which should improve interoperability with ES2015 template literals and with Markdown.</li> 2614<li>Within single-backtick embedded JavaScript, backticks can now be escaped via <code>\`â</code>.</li> 2615<li>The browser tests now run in the browser again, and are accessible <a href="test.html">here</a> if you would like to test your browser.</li> 2616<li>CoffeeScript-only keywords in ES2015 <code>import</code>s and <code>export</code>s are now ignored.</li> 2617<li>The compiler now throws an error on trying to export an anonymous class.</li> 2618<li>Bugfixes related to tokens and location data, for better source maps and improved compatibility with downstream tools.</li> 2619</ul> 2620<div class="anchor" id="1.11.1"></div> 2621<h2 class="header"> 2622 <a href="https://github.com/jashkenas/coffeescript/compare/1.11.0...1.11.1">1.11.1</a> 2623 <span class="timestamp"> — <time datetime="2016-10-02">October 2, 2016</time></span> 2624</h2><ul> 2625<li>Bugfix for shorthand object syntax after interpolated keys.</li> 2626<li>Bugfix for indentation-stripping in <code>"""</code> strings.</li> 2627<li>Bugfix for not being able to use the name âargumentsâ for a prototype property of class.</li> 2628<li>Correctly compile large hexadecimal numbers literals to <code>2e308</code> (just like all other large number literals do).</li> 2629</ul> 2630<div class="anchor" id="1.11.0"></div> 2631<h2 class="header"> 2632 <a href="https://github.com/jashkenas/coffeescript/compare/1.10.0...1.11.0">1.11.0</a> 2633 <span class="timestamp"> — <time datetime="2016-09-24">September 24, 2016</time></span> 2634</h2><ul> 2635<li> 2636<p>CoffeeScript now supports ES2015 <a href="#modules"><code>import</code> and <code>export</code> syntax</a>.</p> 2637</li> 2638<li> 2639<p>Added the <code>-M, --inline-map</code> flag to the compiler, allowing you embed the source map directly into the output JavaScript, rather than as a separate file.</p> 2640</li> 2641<li> 2642<p>A bunch of fixes for <code>yield</code>:</p> 2643<ul> 2644<li> 2645<p><code>yield return</code> can no longer mistakenly be used as an expression.</p> 2646</li> 2647<li> 2648<p><code>yield</code> now mirrors <code>return</code> in that it can be used stand-alone as well as with expressions. Where you previously wrote <code>yield undefined</code>
2648, you may now write simply <code>yield</code>. However, this means also inheriting the same syntax limitations that <code>return</code> has, so these examples no longer compile:</p> 2649<blockquote class="uneditable-code-block"><pre><code>doubles = -> 2650 yield for i in [1..3] 2651 i * 2 2652six = -> 2653 yield 2654 2 * 3 2655</code></pre> 2656</blockquote></li> 2657<li> 2658<p>The JavaScript output is a bit nicer, with unnecessary parentheses and spaces, double indentation and double semicolons around <code>yield</code> no longer present.</p> 2659</li> 2660</ul> 2661</li> 2662<li> 2663<p><code>&&=</code>, <code>||=</code>, <code>and=</code> and <code>or=</code> no longer accidentally allow a space before the equals sign.</p> 2664</li> 2665<li> 2666<p>Improved several error messages.</p> 2667</li> 2668<li> 2669<p>Just like <code>undefined</code> compiles to <code>void 0</code>, <code>NaN</code> now compiles into <code>0/0</code> and <code>Infinity</code> into <code>2e308</code>.</p> 2670</li> 2671<li> 2672<p>Bugfix for renamed destructured parameters with defaults. <code>({a: b = 1}) -></code> no longer crashes the compiler.</p> 2673</li> 2674<li> 2675<p>Improved the internal representation of a CoffeeScript program. This is only noticeable to tools that use <code>CoffeeScript.tokens</code> or <code>CoffeeScript.nodes</code>. Such tools need to update to take account for changed or added tokens and nodes.</p> 2676</li> 2677<li> 2678<p>Several minor bug fixes, including:</p> 2679<ul> 2680<li>The caught error in <code>catch</code> blocks is no longer declared unnecessarily, and no longer mistakenly named <code>undefined</code> for <code>catch</code>-less <code>try</code> blocks.</li> 2681<li>Unassignable parameter destructuring no longer crashes the compiler.</li> 2682<li>Source maps are now used correctly for errors thrown from .coffee.md files.</li> 2683<li><code>coffee -e 'throw null'</code> no longer crashes.</li> 2684<li>The REPL no longer crashes when using <code>.exit</code> to exit it.</li> 2685<li>Invalid JavaScript is no longer output when lots of <code>for</code> loops are used in the same scope.</li> 2686<li>A unicode issue when using stdin with the CLI.</li> 2687</ul> 2688</li> 2689</ul> 2690<div class="anchor" id="1.10.0"></div> 2691<h2 class="header"> 2692 <a href="https://github.com/jashkenas/coffeescript/compare/1.9.3...1.10.0">1.10.0</a> 2693 <span class="timestamp"> — <time datetime="2015-09-03">September 3, 2015</time></span> 2694</h2><ul> 2695<li> 2696<p>CoffeeScript now supports ES2015-style destructuring defaults.</p> 2697</li> 2698<li> 2699<p><code>(offsetHeight: height) -></code> no longer compiles. That syntax was accidental and partly broken. Use <code>({offsetHeight: height}) -></code> instead. Object destructuring always requires braces.</p> 2700</li> 2701<li> 2702<p>Several minor bug fixes, including:</p> 2703<ul> 2704<li>A bug where the REPL would sometimes report valid code as invalid, based on what you had typed earlier.</li> 2705<li>A problem with multiple JS contexts in the jest test framework.</li> 2706<li>An error in io.js where strict mode is set on internal modules.</li> 2707<li>A variable name clash for the caught error in <code>catch</code> blocks.</li> 2708</ul> 2709</li> 2710</ul> 2711<div class="anchor" id="1.9.3"></div> 2712<h2 class="header"> 2713 <a href="https://github.com/jashkenas/coffeescript/compare/1.9.2...1.9.3">1.9.3</a> 2714 <span class="timestamp"> — <time datetime="2015-05-27">May 27, 2015</time></span> 2715</h2><ul> 2716<li>Bugfix for interpolation in the first key of an object literal in an implicit call.</li> 2717<li>Fixed broken error messages in the REPL, as well as a few minor bugs with the REPL.</li> 2718<li>Fixed source mappings for tokens at the beginning of lines when compiling with the <code>--bare</code> option. This has the nice side effect of generating smaller source maps.</li> 2719<li>Slight formatting improvement of compiled block comments.</li> 2720<li>Better error messages for <code>on</code>, <code>off</code>, <code>yes</code> and <code>no</code>.</li> 2721</ul> 2722<div class="anchor" id="1.9.2"></div> 2723<h2 class="header"> 2724 <a href="https://github.com/jashkenas/coffeescript/compare/1.9.1...1.9.2">1.9.2</a> 2725 <span class="timestamp"> — <time datetime="2015-04-15">April 15, 2015</time></span> 2726</h2><ul> 2727<li>Fixed a <strong>watch</strong> mode error introduced in 1.9.1 when compiling multiple files with the same filename.</li> 2728<li>Bugfix for <code>yield</code> around expressions containing <code>this</code>.</li> 2729<li>Added a Ruby-style <code>-r</code> option to the REPL, which allows requiring a module before execution with <code>--eval</code> or <code>--interactive</code>.</li> 2730<li>In <code><script type="text/coffeescript"></code> tags, to avoid possible duplicate browser requests for .coffee files, you can now use the <code>data-src</code> attribute instead of <code>src</code>.</li> 2731<li>Minor bug fixes for IE8, strict ES5 regular expressions and Browserify.</li> 2732</ul> 2733<div class="anchor" id="1.9.1"></div> 2734<h2 class="header"> 2735 <a href="https://github.com/jashkenas/coffeescript/compare/1.9.0...1.9.1">1.9.1</a> 2736 <span class="timestamp"> — <time datetime="2015-02-18">February 18, 2015</time></span> 2737</h2><ul> 2738<li>Interpolation now works in object literal keys (again). You can use this to dynamically name properties.</li> 2739<li>
2739Internal compiler variable names no longer start with underscores. This makes the generated JavaScript a bit prettier, and also fixes an issue with the completely broken and ungodly way that AngularJS âparsesâ function arguments.</li> 2740<li>Fixed a few <code>yield</code>-related edge cases with <code>yield return</code> and <code>yield throw</code>.</li> 2741<li>Minor bug fixes and various improvements to compiler error messages.</li> 2742</ul> 2743<div class="anchor" id="1.9.0"></div> 2744<h2 class="header"> 2745 <a href="https://github.com/jashkenas/coffeescript/compare/1.8.0...1.9.0">1.9.0</a> 2746 <span class="timestamp"> — <time datetime="2015-01-29">January 29, 2015</time></span> 2747</h2><ul> 2748<li>CoffeeScript now supports ES2015 generators. A generator is simply a function that <code>yield</code>s.</li> 2749<li>More robust parsing and improved error messages for strings and regexes â especially with respect to interpolation.</li> 2750<li>Changed strategy for the generation of internal compiler variable names. Note that this means that <code>@example</code> function parameters are no longer available as naked <code>example</code> variables within the function body.</li> 2751<li>Fixed REPL compatibility with latest versions of Node and Io.js.</li> 2752<li>Various minor bug fixes.</li> 2753</ul> 2754<div class="anchor" id="1.8.0"></div> 2755<h2 class="header"> 2756 <a href="https://github.com/jashkenas/coffeescript/compare/1.7.1...1.8.0">1.8.0</a> 2757 <span class="timestamp"> — <time datetime="2014-08-26">August 26, 2014</time></span> 2758</h2><ul> 2759<li>The <code>--join</code> option of the CLI is now deprecated.</li> 2760<li>Source maps now use <code>.js.map</code> as file extension, instead of just <code>.map</code>.</li> 2761<li>The CLI now exits with the exit code 1 when it fails to write a file to disk.</li> 2762<li>The compiler no longer crashes on unterminated, single-quoted strings.</li> 2763<li>Fixed location data for string interpolations, which made source maps out of sync.</li> 2764<li>The error marker in error messages is now correctly positioned if the code is indented with tabs.</li> 2765<li>Fixed a slight formatting error in CoffeeScriptâs source map-patched stack traces.</li> 2766<li>The <code>%%</code> operator now coerces its right operand only once.</li> 2767<li>It is now possible to require CoffeeScript files from Cakefiles without having to register the compiler first.</li> 2768<li>The CoffeeScript REPL is now exported and can be required using <code>require 'coffee-script/repl'</code>.</li> 2769<li>Fixes for the REPL in Node 0.11.</li> 2770</ul> 2771<div class="anchor" id="1.7.1"></div> 2772<h2 class="header"> 2773 <a href="https://github.com/jashkenas/coffeescript/compare/1.7.0...1.7.1">1.7.1</a> 2774 <span class="timestamp"> — <time datetime="2014-01-29">January 29, 2014</time></span> 2775</h2><ul> 2776<li>Fixed a typo that broke node module lookup when running a script directly with the <code>coffee</code> binary.</li> 2777</ul> 2778<div class="anchor" id="1.7.0"></div> 2779<h2 class="header"> 2780 <a href="https://github.com/jashkenas/coffeescript/compare/1.6.3...1.7.0">1.7.0</a> 2781 <span class="timestamp"> — <time datetime="2014-01-28">January 28, 2014</time></span> 2782</h2><ul> 2783<li>When requiring CoffeeScript files in Node you must now explicitly register the compiler. This can be done with <code>require 'coffee-script/register'</code> or <code>CoffeeScript.register()</code>. Also for configuration such as Mochaâs, use <strong>coffee-script/register</strong>.</li> 2784<li>Improved error messages, source maps and stack traces. Source maps now use the updated <code>//#</code> syntax.</li> 2785<li>Leading <code>.</code> now closes all open calls, allowing for simpler chaining syntax.</li> 2786</ul> 2787<div class='code'><pre><code>$ <span class="string">'body'</span> 2788.click (e) -> 2789 $ <span class="string">'.box'</span> 2790 .fadeIn <span class="string">'fast'</span> 2791 .addClass <span class="string">'.active'</span> 2792.css <span class="string">'background'</span>, <span class="string">'white'</span> 2793</code></pre><pre><code>$(<span class="string">'body'</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params">e</span>
2793) </span>{ 2794 <span class="keyword">return</span> $(<span class="string">'.box'</span>).fadeIn(<span class="string">'fast'</span>).addClass(<span class="string">'.active'</span>); 2795}).css(<span class="string">'background'</span>, <span class="string">'white'</span>); 2796</code></pre>
2796<script>window.example1 = "$ 'body'\n.click (e) ->\n $ '.box'\n .fadeIn 'fast'\n .addClass '.active'\n.css 'background', 'white'\n"</script>
2796<div class='minibutton load' onclick='javascript: loadConsole(example1);'>load</div><br class='clear' /></div><ul> 2797<li>Added <code>**</code>, <code>//</code> and <code>%%</code> operators and <code>...</code> expansion in parameter lists and destructuring expressions.</li> 2798<li>Multiline strings are now joined by a single space and ignore all indentation. A backslash at the end of a line can denote the amount of whitespace between lines, in both strings and heredocs. Backslashes correctly escape whitespace in block regexes.</li> 2799<li>Closing brackets can now be indented and therefore no longer cause unexpected error.</li> 2800<li>Several breaking compilation fixes. Non-callable literals (strings, numbers etc.) donât compile in a call now and multiple postfix conditionals compile properly. Postfix conditionals and loops always bind object literals. Conditional assignment compiles properly in subexpressions. <code>super</code> is disallowed outside of methods and works correctly inside <code>for</code> loops.</li> 2801<li>Formatting of compiled block comments has been improved.</li> 2802<li>No more <code>-p</code> folders on Windows.</li> 2803<li>The <code>options</code> object passed to CoffeeScript is no longer mutated.</li> 2804</ul> 2805<div class="anchor" id="1.6.3"></div> 2806<h2 class="header"> 2807 <a href="https://github.com/jashkenas/coffeescript/compare/1.6.2...1.6.3">1.6.3</a> 2808 <span class="timestamp"> — <time datetime="2013-06-02">June 2, 2013</time></span> 2809</h2><ul> 2810<li>The CoffeeScript REPL now remembers your history between sessions. Just like a proper REPL should.</li> 2811<li>You can now use <code>require</code> in Node to load <code>.coffee.md</code> Literate CoffeeScript files. In the browser, <code>text/literate-coffeescript</code> script tags.</li> 2812<li>The old <code>coffee --lint</code> command has been removed. It was useful while originally working on the compiler, but has been surpassed by JSHint. You may now use <code>-l</code> to pass literate files in over <strong>stdio</strong>.</li> 2813<li>Bugfixes for Windows path separators, <code>catch</code> without naming the error, and executable-class-bodies-with- prototypal-property-attachment.</li> 2814</ul> 2815<div class="anchor" id="1.6.2"></div> 2816<h2 class="header"> 2817 <a href="https://github.com/jashkenas/coffeescript/compare/1.6.1...1.6.2">1.6.2</a> 2818 <span class="timestamp"> — <time datetime="2013-03-18">March 18, 2013</time></span> 2819</h2><ul> 2820<li>Source maps have been used to provide automatic line-mapping when running CoffeeScript directly via the <code>coffee</code> command, and for automatic line-mapping when running CoffeeScript directly in the browser. Also, to provide better error messages for semantic errors thrown by the compiler â <a href="http://cl.ly/NdOA">with colors, even</a>.</li> 2821<li>Improved support for mixed literate/vanilla-style CoffeeScript projects, and generating source maps for both at the same time.</li> 2822<li>Fixes for <strong>1.6.x</strong> regressions with overriding inherited bound functions, and for Windows file path management.</li> 2823<li>The <code>coffee</code> command can now correctly <code>fork()</code> both <code>.coffee</code> and <code>.js</code> files. (Requires Node.js 0.9+)</li> 2824</ul> 2825<div class="anchor" id="1.6.1"></div> 2826<h2 class="header"> 2827 <a href="https://github.com/jashkenas/coffeescript/compare/1.5.0...1.6.1">1.6.1</a> 2828 <span class="timestamp"> — <time datetime="2013-03-05">March 5, 2013</time></span> 2829</h2><ul> 2830<li>First release of <a href="#source-maps">source maps</a>. Pass the <code>--map</code> flag to the compiler, and off you go. Direct all your thanks over to <a href="https://github.com/jwalton">Jason Walton</a>.</li> 2831<li>Fixed a 1.5.0 regression with multiple implicit calls against an indented implicit object. Combinations of implicit function calls and implicit objects should generally be parsed better now â but it still isnât good <em>style</em> to nest them too heavily.</li> 2832<li><code>.coffee.md</code> is now also supported as a Literate CoffeeScript file extension, for existing tooling. <code>.litcoffee</code> remains the canonical one.</li> 2833<li>Several minor fixes surrounding member properties, bound methods and <code>super</code> in class declarations.</li> 2834</ul> 2835<div class="anchor" id="1.5.0"></div> 2836<h2 class="header"> 2837 <a href="https://github.com/jashkenas/coffeescript/compare/1.4.0...1.5.0">1.5.0</a> 2838 <span class="timestamp"> — <time datetime="2013-02-25">February 25, 2013</time></span> 2839</h2><ul> 2840<li>First release of <a href="#literate">Literate CoffeeScript</a>.</li> 2841<li>The CoffeeScript REPL is now based on the Node.js REPL, and should work better and more familiarly.</li> 2842<li>Returning explicit values from constructors is now forbidden. If you want to return an arbitrary value, use a function, not a constructor.</li> 2843<li>You can now loop over an array backwards, without having to manually deal with the indexes: <code>for item in list by -1</code></li> 2844<li>Source locations are now preserved in the CoffeeScript AST, although source maps are not yet being emitted.</li> 2845</ul> 2846<div class="anchor" id="1.4.0"></div> 2847<h2 class="header"> 2848 <a href="https://github.com/jashkenas/coffeescript/compare/1.3.3...1.4.0">1.4.0</a> 2849 <span class="timestamp"> — <time datetime="2012-10-23">October 23, 2012</time></span> 2850</h2><ul> 2851<li>The CoffeeScript compiler now strips Microsoftâs UTF-8 BOM if it exists, allowing you to compile BOM-borked source files.</li> 2852<li>Fix Node/compiler deprecation warnings by removing <code>registerExtension</code>, and moving from <code>path.exists</code> to <code>fs.exists</code>.</li> 2853<li>Small tweaks to splat compilation, backticks, slicing, and the error for duplicate keys in object literals.</li> 2854</ul> 2855<div class="anchor" id="1.3.3"></div> 2856<h2 class="header"> 2857 <a href="https://github.com/jashkenas/coffeescript/compare/1.3.1...1.3.3">1.3.3</a> 2858 <span class="timestamp"> — <time datetime="2012-05-15">May 15, 2012</time></span> 2859</h2><ul> 2860<li>Due to the new semantics of JavaScriptâs strict mode, CoffeeScript no longer guarantees that constructor functions have names in all runtimes. See <a href="https://github.com/jashkenas/coffeescript/issues/2052">#2052</a> for discussion.</li> 2861<li>Inside of a nested function inside of an instance method, itâs now possible to call <code>super</code> more reliably (walks recursively up).</li> 2862<li>Named loop variables no longer have different scoping heuristics than other local variables. (Reverts #643)</li> 2863<li>Fix for splats nested within the LHS of destructuring assignment.</li> 2864<li>Corrections to our compile time strict mode forbidding of octal literals.</li> 2865</ul> 2866<div class="anchor" id="1.3.1"></div> 2867<h2 class="header"> 2868 <a href="https://github.com/jashkenas/coffeescript/compare/1.2.0...1.3.1">1.3.1</a> 2869 <span class="timestamp"> — <time datetime="2012-04-10">April 10, 2012</time></span> 2870</h2><ul> 2871<li>CoffeeScript now enforces all of JavaScriptâs <strong>Strict Mode</strong> early syntax errors at compile time. This includes old-style octal literals, duplicate property names in object literals, duplicate parameters in a function definition, deleting naked variables, setting the value of <code>eval</code> or <code>arguments</code>, and more. See a full discussion at <a href="https://github.com/jashkenas/coffeescript/issues/1547">#1547</a>.</li> 2872<li>The REPL now has a handy new multi-line mode for entering large blocks of code. Itâs useful when copy-and-pasting examples into the REPL. Enter multi-line mode with <code>Ctrl-V</code>. You may also now pipe input directly into the REPL.</li> 2873<li>CoffeeScript now prints a <code>Generated by CoffeeScript VERSION</code> header at the top of each compiled file.</li> 2874<li>Conditional assignment of previously undefined variables <code>a or= b</code> is now considered a syntax error.</li> 2875<li>A tweak to the semantics of <code>do</code>, which can now be used to more easily simulate a namespace: <code>do (x = 1, y = 2) -> â¦</code></li> 2876<li>Loop indices are now mutable within a loop iteration, and immutable between them.</li> 2877<li>Both endpoints of a slice are now allowed to be omitted for consistency, effectively creating a shallow copy of the list.</li> 2878<li>Additional tweaks and improvements to <code>coffee --watch</code> under Nodeâs ânewâ file watching API. Watch will now beep by default if you introduce a syntax error into a watched script. We also now ignore hidden directories by default when watching recursively.</li> 2879</ul> 2880<div class="anchor" id="1.2.0"></div> 2881<h2 class="header"> 2882 <a href="https://github.com/jashkenas/coffeescript/compare/1.1.3...1.2.0">1.2.0</a> 2883 <span class="timestamp"> — <time datetime="2011-12-18">December 18, 2011</time></span> 2884</h2><ul> 2885<li>Multiple improvements to <code>coffee --watch</code> and <code>--join</code>. You may now use both together, as well as add and remove files and directories within a <code>--watch</code>âd folder.</li> 2886<li>The <code>throw</code> statement can now be used as part of an expression.</li> 2887<li>Block comments at the top of the file will now appear outside of the safety closure wrapper.</li> 2888<li>Fixed a number of minor 1.1.3 regressions having to do with trailing operators and unfinished lines, and a more major 1.1.3 regression that caused boun
2888d functions <em>within</em> bound class functions to have the incorrect <code>this</code>.</li> 2889</ul> 2890<div class="anchor" id="1.1.3"></div> 2891<h2 class="header"> 2892 <a href="https://github.com/jashkenas/coffeescript/compare/1.1.2...1.1.3">1.1.3</a> 2893 <span class="timestamp"> — <time datetime="2011-11-08">November 8, 2011</time></span> 2894</h2><ul> 2895<li>Ahh, whitespace. CoffeeScriptâs compiled JS now tries to space things out and keep it readable, as you can see in the examples on this page.</li> 2896<li>You can now call <code>super</code> in class level methods in class bodies, and bound class methods now preserve their correct context.</li> 2897<li>JavaScript has always supported octal numbers <code>010 is 8</code>, and hexadecimal numbers <code>0xf is 15</code>, but CoffeeScript now also supports binary numbers: <code>0b10 is 2</code>.</li> 2898<li>The CoffeeScript module has been nested under a subdirectory to make it easier to <code>require</code> individual components separately, without having to use <strong>npm</strong>. For example, after adding the CoffeeScript folder to your path: <code>require('coffee-script/lexer')</code></li> 2899<li>Thereâs a new âlinkâ feature in Try CoffeeScript on this webpage. Use it to get a shareable permalink for your example script.</li> 2900<li>The <code>coffee --watch</code> feature now only works on Node.js 0.6.0 and higher, but now also works properly on Windows.</li> 2901<li>Lots of small bug fixes from <strong><a href="https://github.com/michaelficarra">@michaelficarra</a></strong>, <strong><a href="https://github.com/geraldalewis">@geraldalewis</a></strong>, <strong><a href="https://github.com/satyr">@satyr</a></strong>, and <strong><a href="https://github.com/trevorburnham">@trevorburnham</a></strong>.</li> 2902</ul> 2903<div class="anchor" id="1.1.2"></div> 2904<h2 class="header"> 2905 <a href="https://github.com/jashkenas/coffeescript/compare/1.1.1...1.1.2">1.1.2</a> 2906 <span class="timestamp"> — <time datetime="2011-08-04">August 4, 2011</time></span> 2907</h2><p>Fixes for block comment formatting, <code>?=</code> compilation, implicit calls against control structures, implicit invocation of a try/catch block, variadic arguments leaking from local scope, line numbers in syntax errors following heregexes, property access on parenthesized number literals, bound class methods and super with reserved names, a REPL overhaul, consecutive compiled semicolons, block comments in implicitly called objects, and a Chrome bug.</p> 2908<div class="anchor" id="1.1.1"></div> 2909<h2 class="header"> 2910 <a href="https://github.com/jashkenas/coffeescript/compare/1.1.0...1.1.1">1.1.1</a> 2911 <span class="timestamp"> — <time datetime="2011-05-10">May 10, 2011</time></span> 2912</h2><p>Bugfix release for classes with external constructor functions, see issue #1182.</p> 2913<div class="anchor" id="1.1.0"></div> 2914<h2 class="header"> 2915 <a href="https://github.com/jashkenas/coffeescript/compare/1.0.1...1.1.0">1.1.0</a> 2916 <span class="timestamp"> — <time datetime="2011-05-01">May 1, 2011</time></span> 2917</h2><p>When running via the <code>coffee</code> executable, <code>process.argv</code> and friends now report <code>coffee</code> instead of <code>node</code>. Better compatibility with <strong>Node.js 0.4.x</strong> module lookup changes. The output in the REPL is now colorized, like Nodeâs is. Giving your concatenated CoffeeScripts a name when using <code>--join</code> is now mandatory. Fix for lexing compound division <code>/=</code> as a regex accidentally. All <code>text/coffeescript</code> tags should now execute in the order theyâre included. Fixed an issue with extended subclasses using external constructor functions. Fixed an edge-case infinite loop in <code>addImplicitParentheses</code>. Fixed exponential slowdown with long chains of function calls. Globals no longer leak into the CoffeeScript REPL. Splatted parameters are declared local to the function.</p> 2918<div class="anchor" id="1.0.1"></div> 2919<h2 class="header"> 2920 <a href="https://github.com/jashkenas/coffeescript/compare/1.0.0...1.0.1">1.0.1</a> 2921 <span class="timestamp"> — <time datetime="2011-01-31">January 31, 2011</time></span> 2922</h2><p>Fixed a lexer bug with Unicode identifiers. Updated REPL for compatibility with Node.js 0.3.7. Fixed requiring relative paths in the REPL. Trailing <code>return</code> and <code>return undefined</code> are now optimized away. Stopped requiring the core Node.js <code>util</code> module for back-compatibility with Node.js 0.2.5. Fixed a case where a conditional <code>return</code> would cause fallthrough in a <code>switch</code> statement. Optimized empty objects in destructuring assignment.</p> 2923<div class="anchor" id="1.0.0"></div> 2924<h2 class="header"> 2925 <a href="https://github.com/jashkenas/coffeescript/compare/0.9.6...1.0.0">1.0.0</a> 2926 <span class="timestamp"> — <time datetime="2010-12-24">December 24, 2010</time></span> 2927</h2><p>CoffeeScript loops no longer try to preserve block scope when functions are being generated
2927within the loop body. Instead, you can use the <code>do</code> keyword to create a convenient closure wrapper. Added a <code>--nodejs</code> flag for passing through options directly to the <code>node</code> executable. Better behavior around the use of pure statements within expressions. Fixed inclusive slicing through <code>-1</code>, for all browsers, and splicing with arbitrary expressions as endpoints.</p> 2928<div class="anchor" id="0.9.6"></div> 2929<h2 class="header"> 2930 <a href="https://github.com/jashkenas/coffeescript/compare/0.9.5...0.9.6">0.9.6</a> 2931 <span class="timestamp"> — <time datetime="2010-12-06">December 6, 2010</time></span> 2932</h2><p>The REPL now properly formats stacktraces, and stays alive through asynchronous exceptions. Using <code>--watch</code> now prints timestamps as files are compiled. Fixed some accidentally-leaking variables within plucked closure-loops. Constructors now maintain their declaration location within a class body. Dynamic object keys were removed. Nested classes are now supported. Fixes execution context for naked splatted functions. Bugfix for inversion of chained comparisons. Chained class instantiation now works properly with splats.</p> 2933<div class="anchor" id="0.9.5"></div> 2934<h2 class="header"> 2935 <a href="https://github.com/jashkenas/coffeescript/compare/0.9.4...0.9.5">0.9.5</a> 2936 <span class="timestamp"> — <time datetime="2010-11-21">November 21, 2010</time></span> 2937</h2><p>0.9.5 should be considered the first release candidate for CoffeeScript 1.0. There have been a large number of internal changes since the previous release, many contributed from <strong>satyr</strong>âs <a href="https://github.com/satyr/coco">Coco</a> dialect of CoffeeScript. Heregexes (extended regexes) were added. Functions can now have default arguments. Class bodies are now executable code. Improved syntax errors for invalid CoffeeScript. <code>undefined</code> now works like <code>null</code>, and cannot be assigned a new value. There was a precedence change with respect to single-line comprehensions: <code>result = i for i in list</code> 2938used to parse as <code>result = (i for i in list)</code> by default ⦠it now parses as 2939<code>(result = i) for i in list</code>.</p> 2940<div class="anchor" id="0.9.4"></div> 2941<h2 class="header"> 2942 <a href="https://github.com/jashkenas/coffeescript/compare/0.9.3...0.9.4">0.9.4</a> 2943 <span class="timestamp"> — <time datetime="2010-09-21">September 21, 2010</time></span> 2944</h2><p>CoffeeScript now uses appropriately-named temporary variables, and recycles their references after use. Added <code>require.extensions</code> support for <strong>Node.js 0.3</strong>. Loading CoffeeScript in the browser now adds just a single <code>CoffeeScript</code> object to global scope. Fixes for implicit object and block comment edge cases.</p> 2945<div class="anchor" id="0.9.3"></div> 2946<h2 class="header"> 2947 <a href="https://github.com/jashkenas/coffeescript/compare/0.9.2...0.9.3">0.9.3</a> 2948 <span class="timestamp"> — <time datetime="2010-09-16">September 16, 2010</time></span> 2949</h2><p>CoffeeScript <code>switch</code> statements now compile into JS <code>switch</code> statements â they previously compiled into <code>if/else</code> chains for JavaScript 1.3 compatibility. Soaking a function invocation is now supported. Users of the RubyMine editor should now be able to use <code>--watch</code> mode.</p> 2950<div class="anchor" id="0.9.2"></div> 2951<h2 class="header"> 2952 <a href="https://github.com/jashkenas/coffeescript/compare/0.9.1...0.9.2">0.9.2</a> 2953 <span class="timestamp"> — <time datetime="2010-08-23">August 23, 2010</time></span> 2954</h2><p>Specifying the start and end of a range literal is now optional, eg. <code>array[3..]</code>. You can now say <code>a not instanceof b</code>. Fixed important bugs with nested significant and non-significant indentation (Issue #637). Added a <code>--require</code> flag that allows you to hook into the <code>coffee</code> command. Added a custom <code>jsl.conf</code> file for our preferred JavaScriptLint setup. Sped up Jison grammar compilation time by flattening rules for operations. Block comments can now be used with JavaScript-minifier-friendly syntax. Added JavaScriptâs compound assignment bitwise operators. Bugfixes to implicit object literals with leading number and string keys, as the subject of implicit calls, and as part of compound assignment.</p> 2955<div class="anchor" id="0.9.1"></div> 2956<h2 class="header"> 2957 <a href="https://github.com/jashkenas/coffeescript/compare/0.9.0...0.9.1">0.9.1</a> 2958 <span class="timestamp"> — <time datetime="2010-08-11">August 11, 2010</time></span> 2959</h2><p>Bugfix release for <strong>0.9.1</strong>. Greatly improves the handling of mixed implicit objects, implicit function calls, and implicit indentation. String and regex interpolation is now strictly <code>#{ ⦠}</code> (Ruby style). The compiler now takes a <code>--require</code> flag, which specifies scripts to run before compilation.</p> 2960<div class="anchor" id="0.9.0"></div> 2961<h2 class="header"> 2962 <a href="https://github.com/jashkenas/coffeescript/compare/0.7.2...0.9.0">0.9.0</a> 2963 <span class="timestamp"> — <time datetime="2010-08-04">August 4, 2010</time></span> 2964</h2><p>The CoffeeScript <strong>0.9</strong> series is considered to be a release candidate for <strong>1.0</strong>; letâs give her a shakedown cruise. <strong>0.9.0</strong> introduces a massive backwards-incompatible change: Assignment now uses <code>=</code>, and object literals use <code>:</code>, as in JavaScript. This allows us to have implicit object literals, and YAML-style object definitions. Half assignments are removed, in favor of <code>+=</code>, <code>or=</code>, and friends. Interpolation now uses a hash mark <code>#</code> instead of the dollar sign <code>$</code> â because dollar signs may be part of a valid JS identifier. Downwards range comprehensions are now safe again, and are optimized to straight for loops when created with integer endpoints. A fast, unguarded form of object c
2964omprehension was added: <code>for all key, value of object</code>. Mentioning the <code>super</code> keyword with no arguments now forwards all arguments passed to the function, as in Ruby. If you extend class <code>B</code> from parent class <code>A</code>, if <code>A</code> has an <code>extended</code> method defined, it will be called, passing in <code>B</code> â this enables static inheritance, among other things. Cleaner output for functions bound with the fat arrow. <code>@variables</code> can now be used in parameter lists, with the parameter being automatically set as a property on the object â useful in constructors and setter functions. Constructor functions can now take splats.</p> 2965<div class="anchor" id="0.7.2"></div> 2966<h2 class="header"> 2967 <a href="https://github.com/jashkenas/coffeescript/compare/0.7.1...0.7.2">0.7.2</a> 2968 <span class="timestamp"> — <time datetime="2010-07-12">July 12, 2010</time></span> 2969</h2><p>Quick bugfix (right after 0.7.1) for a problem that prevented <code>coffee</code> command-line options from being parsed in some circumstances.</p> 2970<div class="anchor" id="0.7.1"></div> 2971<h2 class="header"> 2972 <a href="https://github.com/jashkenas/coffeescript/compare/0.7.0...0.7.1">0.7.1</a> 2973 <span class="timestamp"> — <time datetime="2010-07-11">July 11, 2010</time></span> 2974</h2><p>Block-style comments are now passed through and printed as JavaScript block comments â making them useful for licenses and copyright headers. Better support for running coffee scripts standalone via hashbangs. Improved syntax errors for tokens that are not in the grammar.</p> 2975<div class="anchor" id="0.7.0"></div> 2976<h2 class="header"> 2977 <a href="https://github.com/jashkenas/coffeescript/compare/0.6.2...0.7.0">0.7.0</a> 2978 <span class="timestamp"> — <time datetime="2010-06-28">June 28, 2010</time></span> 2979</h2><p>Official CoffeeScript variable style is now camelCase, as in JavaScript. Reserved words are now allowed as object keys, and will be quoted for you. Range comprehensions now generate cleaner code, but you have to specify <code>by -1</code> if youâd like to iterate downward. Reporting of syntax errors is greatly improved from the previous release. Running <code>coffee</code> with no arguments now launches the REPL, with Readline support. The <code><-</code> bind operator has been removed from CoffeeScript. The <code>loop</code> keyword was added, which is equivalent to a <code>while true</code> loop. Comprehensions that contain closures will now close over their variables, like the semantics of a <code>forEach</code>. You can now use bound function in class definitions (bound to the instance). For consistency, <code>a in b</code> is now an array presence check, and <code>a of b</code> is an object-key check. Comments are no longer passed through to the generated JavaScript.</p> 2980<div class="anchor" id="0.6.2"></div> 2981<h2 class="header"> 2982 <a href="https://github.com/jashkenas/coffeescript/compare/0.6.1...0.6.2">0.6.2</a> 2983 <span class="timestamp"> — <time datetime="2010-05-15">May 15, 2010</time></span> 2984</h2><p>The <code>coffee</code> command will now preserve directory structure when compiling a directory full of scripts. Fixed two omissions that were preventing the CoffeeScript compiler from running live within Internet Explorer. Thereâs now a syntax for block comments, similar in spirit to CoffeeScriptâs heredocs. ECMA Harmony DRY-style pattern matching is now supported, where the name of the property is the same as the name of the value: <code>{name, length}: func</code>. Pattern matching is now allowed within comprehension variables. <code>unless</code> is now allowed in block form. <code>until</code> loops were added, as the inverse of <code>while</code> loops. <code>switch</code> statements are now allowed without switch object clauses. Compatible with Node.js <strong>v0.1.95</strong>.</p> 2985<div class="anchor" id="0.6.1"></div> 2986<h2 class="header"> 2987 <a href="https://github.com/jashkenas/coffeescript/compare/0.6.0...0.6.1">0.6.1</a> 2988 <span class="timestamp"> — <time datetime="2010-04-12">April 12, 2010</time></span> 2989</h2><p>Upgraded CoffeeScript for compatibility with the new Node.js <strong>v0.1.90</strong> series.</p> 2990<div class="anchor" id="0.6.0"></div> 2991<h2 class="header"> 2992 <a href="https://github.com/jashkenas/coffeescript/compare/0.5.6...0.6.0">
29920.6.0</a> 2993 <span class="timestamp"> — <time datetime="2010-04-03">April 3, 2010</time></span> 2994</h2><p>Trailing commas are now allowed, a-la Python. Static properties may be assigned directly within class definitions, using <code>@property</code> notation.</p> 2995<div class="anchor" id="0.5.6"></div> 2996<h2 class="header"> 2997 <a href="https://github.com/jashkenas/coffeescript/compare/0.5.5...0.5.6">0.5.6</a> 2998 <span class="timestamp"> — <time datetime="2010-03-23">March 23, 2010</time></span> 2999</h2><p>Interpolation can now be used within regular expressions and heredocs, as well as strings. Added the <code><-</code> bind operator. Allowing assignment to half-expressions instead of special <code>||=</code>-style operators. The arguments object is no longer automatically converted into an array. After requiring <code>coffee-script</code>, Node.js can now directly load <code>.coffee</code> files, thanks to <strong>registerExtension</strong>. Multiple splats can now be used in function calls, arrays, and pattern matching.</p> 3000<div class="anchor" id="0.5.5"></div> 3001<h2 class="header"> 3002 <a href="https://github.com/jashkenas/coffeescript/compare/0.5.4...0.5.5">0.5.5</a> 3003 <span class="timestamp"> — <time datetime="2010-03-08">March 8, 2010</time></span> 3004</h2><p>String interpolation, contributed by <a href="https://github.com/StanAngeloff">Stan Angeloff</a>. Since <code>--run</code> has been the default since <strong>0.5.3</strong>, updating <code>--stdio</code> and <code>--eval</code> to run by default, pass <code>--compile</code> as well if youâd like to print the result.</p> 3005<div class="anchor" id="0.5.4"></div> 3006<h2 class="header"> 3007 <a href="https://github.com/jashkenas/coffeescript/compare/0.5.3...0.5.4">0.5.4</a> 3008 <span class="timestamp"> — <time datetime="2010-03-03">March 3, 2010</time></span> 3009</h2><p>Bugfix that corrects the Node.js global constants <code>__filename</code> and <code>__dirname</code>. Tweaks for more flexible parsing of nested function literals and improperly-indented comments. Updates for the latest Node.js API.</p> 3010<div class="anchor" id="0.5.3"></div> 3011<h2 class="header"> 3012 <a href="https://github.com/jashkenas/coffeescript/compare/0.5.2...0.5.3">0.5.3</a> 3013 <span class="timestamp"> — <time datetime="2010-02-27">February 27, 2010</time></span> 3014</h2><p>CoffeeScript now has a syntax for defining classes. Many of the core components (Nodes, Lexer, Rewriter, Scope, Optparse) are using them. Cakefiles can use <code>optparse.coffee</code> to define options for tasks. <code>--run</code> is now the default flag for the <code>coffee</code> command, use <code>--compile</code> to save JavaScripts. Bugfix for an ambiguity between RegExp literals and chained divisions.</p> 3015<div class="anchor" id="0.5.2"></div> 3016<h2 class="header"> 3017 <a href="https://github.com/jashkenas/coffeescript/compare/0.5.1...0.5.2">0.5.2</a> 3018 <span class="timestamp"> — <time datetime="2010-02-25">February 25, 2010</time></span> 3019</h2><p>Added a compressed version of the compiler for inclusion in web pages as 3020<code>browser-compiler/coffee-script.js</code>. Itâll automatically run any script tags with type <code>text/coffeescript</code> for you. Added a <code>--stdio</code> option to the <code>coffee</code> command, for piped-in compiles.</p> 3021<div class="anchor" id="0.5.1"></div> 3022<h2 class="header"> 3023 <a href="https://github.com/jashkenas/coffeescript/compare/0.5.0...0.5.1">0.5.1</a> 3024 <span class="timestamp"> — <time datetime="2010-02-24">February 24, 2010</time></span> 3025</h2><p>Improvements to null soaking with the existential operator, including soaks on indexed properties. Added conditions to <code>while</code> loops, so you can use them as filters with <code>when</code>, in the same manner as comprehensions.</p> 3026<div class="anchor" id="0.5.0"></div> 3027<h2 class="header"> 3028 <a href="https://github.com/jashkenas/coffeescript/compare/0.3.2...0.5.0">0.5.0</a> 3029 <span class="timestamp"> — <time datetime="2010-02-21">February 21, 2010</time></span> 3030</h2><p>CoffeeScript 0.5.0 is a major release, While there are no language changes, the Ruby compiler has been removed in favor of a self-hosting compiler written in pure CoffeeScript.</p> 3031<div class="anchor" id="0.3.2"></div> 3032<h2 class="header"> 3033 <a href="https://github.com/jashkenas/coffeescript/compare/0.3.0...0.3.2">0.3.2</a> 3034 <span class="timestamp"> — <time datetime="2010-02-08">February 8, 2010</time></span> 3035</h2><p><code>@property</code> is now a shorthand for <code>this.property</code>. 3036Switched the default JavaScript engine from Narwhal to Node.js. Pass the <code>--narwhal</code> flag if youâd like to continue using it.</p> 3037<div class="anchor" id="0.3.0"></div> 3038<h2 class="header"> 3039 <a href="https://github.com/jashkenas/coffeescript/compare/0.2.6...0.3.0">0.3.0</a> 3040 <span class="timestamp"> — <time datetime="2010-01-26">January 26, 2010</time></span> 3041</h2><p>CoffeeScript 0.3 includes major syntax changes: 3042The function symbol was changed to <code>-></code>, and the bound function symbol is now <code>=></code>. 3043Parameter lists in function definitions must now be wrapped in parentheses. 3044Added property soaking, with the <code>?.</code> operator. 3045Made parentheses optional, when invoking functions with arguments. 3046Removed the obsolete block literal syntax.</p> 3047<div class="anchor" id="0.2.6"></div> 3048<h2 class="header"> 3049 <a href="https://github.com/jashkenas/coffeescript/compare/0.2.5...0.2.6">0.2.6</a> 3050 <span class="timestamp"> — <time datetime="2010-01-17">January 17, 2010</time></span> 3051</h2><p>Added Python-style chained comparisons, the conditional existence operator <code>?=</code>, and some examples from <em>Beautiful Code</em>. Bugfixes relating to statement-to-expression conversion, arguments-to-array conversion, and the TextMate syntax highlighter.</p> 3052<div class="anchor" id="0.2.5"></div> 3053<h2 class="header"> 3054 <a href="https://github.com/jashkenas/coffeescript/compare/0.2.4...0.2.5">0.2.5</a> 3055 <span class="timestamp"> — <time datetime="2010-01-13">January 13, 2010</time></span> 3056</h2><p>The conditions in switch statements can now take multiple values at once â If any of them are true, the case will run. Added the long arrow <code>==></code>, which defines and immediately binds a function to <code>this</code>. While loops can now be used as expressions, in the same way that comprehensions can. Splats can be used within pattern matches to soak up the rest of an array.</p> 3057<div class="anchor" id="0.2.4"></div> 3058<h2 class="header"> 3059 <a href="https://github.com/jashkenas/coffeescript/compare/0.2.3...0.2.4">
30590.2.4</a> 3060 <span class="timestamp"> — <time datetime="2010-01-12">January 12, 2010</time></span> 3061</h2><p>Added ECMAScript Harmony style destructuring assignment, for dealing with extracting values from nested arrays and objects. Added indentation-sensitive heredocs for nicely formatted strings or chunks of code.</p> 3062<div class="anchor" id="0.2.3"></div> 3063<h2 class="header"> 3064 <a href="https://github.com/jashkenas/coffeescript/compare/0.2.2...0.2.3">0.2.3</a> 3065 <span class="timestamp"> — <time datetime="2010-01-11">January 11, 2010</time></span> 3066</h2><p>Axed the unsatisfactory <code>ino</code> keyword, replacing it with <code>of</code> for object comprehensions. They now look like: <code>for prop, value of object</code>.</p> 3067<div class="anchor" id="0.2.2"></div> 3068<h2 class="header"> 3069 <a href="https://github.com/jashkenas/coffeescript/compare/0.2.1...0.2.2">0.2.2</a> 3070 <span class="timestamp"> — <time datetime="2010-01-10">January 10, 2010</time></span> 3071</h2><p>When performing a comprehension over an object, use <code>ino</code>, instead of <code>in</code>, which helps us generate smaller, more efficient code at compile time. 3072Added <code>::</code> as a shorthand for saying <code>.prototype.</code> 3073The âsplatâ symbol has been changed from a prefix asterisk <code>*</code>, to a postfix ellipsis <code>...</code> 3074Added JavaScriptâs <code>in</code> operator, empty <code>return</code> statements, and empty <code>while</code> loops. 3075Constructor functions that start with capital letters now include a safety check to make sure that the new instance of the object is returned. 3076The <code>extends</code> keyword now functions identically to <code>goog.inherits</code> in Googleâs Closure Library.</p> 3077<div class="anchor" id="0.2.1"></div> 3078<h2 class="header"> 3079 <a href="https://github.com/jashkenas/coffeescript/compare/0.2.0...0.2.1">0.2.1</a> 3080 <span class="timestamp"> — <time datetime="2010-01-05">January 5, 2010</time></span> 3081</h2><p>Arguments objects are now converted into real arrays when referenced.</p> 3082<div class="anchor" id="0.2.0"></div> 3083<h2 class="header"> 3084 <a href="https://github.com/jashkenas/coffeescript/compare/0.1.6...0.2.0">0.2.0</a> 3085 <span class="timestamp"> — <time datetime="2010-01-05">January 5, 2010</time></span> 3086</h2><p>Major release. Significant whitespace. Better statement-to-expression conversion. Splats. Splice literals. Object comprehensions. Blocks. The existential operator. Many thanks to all the folks who posted issues, with special thanks to <a href="https://github.com/liamoc">Liam OâConnor-Davis</a> for whitespace and expression help.</p> 3087<div class="anchor" id="0.1.6"></div> 3088<h2 class="header"> 3089 <a href="https://github.com/jashkenas/coffeescript/compare/0.1.5...0.1.6">0.1.6</a> 3090 <span class="timestamp"> — <time datetime="2009-12-27">December 27, 2009</time></span> 3091</h2><p>Bugfix for running <code>coffee --interactive</code> and <code>--run</code> from outside of the CoffeeScript directory. Bugfix for nested function/if-statements.</p> 3092<div class="anchor" id="0.1.5"></div> 3093<h2 class="header"> 3094 <a href="https://github.com/jashkenas/coffeescript/compare/0.1.4...0.1.5">0.1.5</a> 3095 <span class="timestamp"> — <time datetime="2009-12-26">December 26, 2009</time></span> 3096</h2><p>Array slice literals and array comprehensions can now both take Ruby-style ranges to specify the start and end. JavaScript variable declaration is now pushed up to the top of the scope, making all assignment statements into expressions. You can use <code>\</code> to escape newlines. The <code>coffee-script</code> command is now called <code>coffee</code>.</p> 3097<div class="anchor" id="0.1.4"></div> 3098<h2 class="header"> 3099 <a href="https://github.com/jashkenas/coffeescript/compare/0.1.3...0.1.4">0.1.4</a> 3100 <span class="timestamp"> — <time datetime="2009-12-25">December 25, 2009</time></span> 3101</h2><p>The official CoffeeScript extension is now <code>.coffee</code> instead of <code>.cs</code>, which properly belongs to <a href="https://en.wikipedia.org/wiki/C_Sharp_(programming_language)">C#</a>. Due to popular demand, you can now also use <code>=</code> to assign. Unlike JavaScript, <code>=</code> can also be used within object literals, interchangeably with <code>:</code>. Made a grammatical fix for chained function calls like <code>func(1)(2)(3)(4)</code>. Inheritance and super no longer use <code>__proto__</code>, so they should be IE-compatible now.</p> 3102<div class="anchor" id="0.1.3"></div> 3103<h2 class="header"> 3104 <a href="https://github.com/jashkenas/coffeescript/compare/0.1.2...0.1.3">0.1.3</a> 3105 <span class="timestamp"> — <time datetime="2009-12-25">December 25, 2009</time></span> 3106</h2><p>The <code>coffee</code> command now includes <code>--interactive</code>
3106, which launches an interactive CoffeeScript session, and <code>--run</code>, which directly compiles and executes a script. Both options depend on a working installation of Narwhal. The <code>aint</code> keyword has been replaced by <code>isnt</code>, which goes together a little smoother with <code>is</code>. Quoted strings are now allowed as identifiers within object literals: eg. <code>{"5+5": 10}</code>. All assignment operators now use a colon: <code>+:</code>, <code>-:</code>, <code>*:</code>, etc.</p> 3107<div class="anchor" id="0.1.2"></div> 3108<h2 class="header"> 3109 <a href="https://github.com/jashkenas/coffeescript/compare/0.1.1...0.1.2">0.1.2</a> 3110 <span class="timestamp"> — <time datetime="2009-12-24">December 24, 2009</time></span> 3111</h2><p>Fixed a bug with calling <code>super()</code> through more than one level of inheritance, with the re-addition of the <code>extends</code> keyword. Added experimental <a href="http://narwhaljs.org/">Narwhal</a> support (as a Tusk package), contributed by <a href="http://blog.tlrobinson.net/">Tom Robinson</a>, including <strong>bin/cs</strong> as a CoffeeScript REPL and interpreter. New <code>--no-wrap</code> option to suppress the safety function wrapper.</p> 3112<div class="anchor" id="0.1.1"></div> 3113<h2 class="header"> 3114 <a href="https://github.com/jashkenas/coffeescript/compare/0.1.0...0.1.1">0.1.1</a> 3115 <span class="timestamp"> — <time datetime="2009-12-24">December 24, 2009</time></span> 3116</h2><p>Added <code>instanceof</code> and <code>typeof</code> as operators.</p> 3117<div class="anchor" id="0.1.0"></div> 3118<h2 class="header"> 3119 <a href="https://github.com/jashkenas/coffeescript/compare/8e9d637985d2dc9b44922076ad54ffef7fa8e9c2...0.1.0">0.1.0</a> 3120 <span class="timestamp"> — <time datetime="2009-12-24">December 24, 2009</time></span> 3121</h2><p>Initial CoffeeScript release.</p> 3122 3123</div> 3124 3125
3126<script type="text/coffeescript"> 3127sourceFragment = "try:" 3128 3129# Set up the compilation function, to run when you stop typing. 3130compileSource = -> 3131 source = $('#repl_source').val() 3132 results = $('#repl_results') 3133 window.compiledJS = '' 3134 try 3135 window.compiledJS = CoffeeScript.compile source, bare: on 3136 el = results[0] 3137 if el.innerText 3138 el.innerText = window.compiledJS 3139 else 3140 results.text(window.compiledJS) 3141 results.removeClass 'error' 3142 $('.minibutton.run').removeClass 'error' 3143 catch {location, message} 3144 if location? 3145 message = "Error on line #{location.first_line + 1}: #{message}" 3146 results.text(message).addClass 'error' 3147 $('.minibutton.run').addClass 'error' 3148 3149 # Update permalink 3150 $('#repl_permalink').attr 'href', "##{sourceFragment}#{encodeURIComponent source}" 3151 3152# Listen for keypresses and recompile. 3153$('#repl_source').keyup -> compileSource() 3154 3155# Use tab key to insert tabs 3156$('#repl_source').keydown (e) -> 3157 if e.keyCode is 9 3158 e.preventDefault() 3159 textbox = e.target 3160 # Insert tab character at caret or in selection 3161 textbox.value = textbox.value[0...textbox.selectionStart] + "\t" + textbox.value[textbox.selectionEnd...] 3162 # Put caret in correct position 3163 textbox.selectionEnd = ++textbox.selectionStart 3164 3165# Eval the compiled js. 3166evalJS = -> 3167 try 3168 eval window.compiledJS 3169 catch error then alert error 3170 3171# Load the console with a string of CoffeeScript. 3172window.loadConsole = (coffee) -> 3173 $('#repl_source').val coffee 3174 compileSource() 3175 $('.navigation.try').addClass('active') 3176 false 3177 3178# Helper to hide the menus. 3179closeMenus = -> 3180 $('.navigation.active').removeClass 'active' 3181 3182$('.minibutton.run').click -> evalJS() 3183 3184# Bind navigation buttons to open the menus. 3185$('.navigation').click (e) -> 3186 return if e.target.tagName.toLowerCase() is 'a' 3187 return false if $(e.target).closest('.repl_wrapper').length 3188 if $(this).hasClass('active') 3189 closeMenus() 3190 else 3191 closeMenus() 3192 $(this).addClass 'active' 3193 false 3194 3195$(document).on 'click', '[href="#try"]', (e) -> 3196 $('.navigation.try').addClass 'active' 3197 3198# Dismiss console if Escape pressed or click falls outside console 3199# Trigger Run button on Ctrl-Enter 3200$(document.body) 3201 .keydown (e) -> 3202 closeMenus() if e.which == 27 3203 evalJS() if e.which == 13 and (e.metaKey or e.ctrlKey) and $('.minibutton.run:visible').length 3204 .click (e) -> 3205 return false if $(e.target).hasClass('minibutton') 3206 closeMenus() 3207 3208$('#open_webchat').click -> 3209 $(this).replaceWith $('<iframe src="http://webchat.freenode.net/?channels=coffeescript" width="625" height="400"></iframe>') 3210 3211$("#repl_permalink").click (e) -> 3212 window.location = $(this).attr("href") 3213 false 3214 3215# If source code is included in location.hash, display it. 3216hash = decodeURIComponent location.hash.replace(/^#/, '') 3217if hash.indexOf(sourceFragment) == 0 3218 src = hash.substr sourceFragment.length 3219 loadConsole src 3220 3221compileSource() 3222 3223</script>
3223 3224
3225<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
vendor: 1 bytes, line 3225
3225
3226<script src="browser-compiler/coffee-script.js"></script>
3226 3227
3228<script type="module" src="https://static.cloudflareinsights.com/beacon.min.js/v31edd6df95cf4e85bb4c19e7a9bdbcba1788362987495" integrity="sha512-iIg7k2xntmwu6/uSb5tpc/hySgZc4eoL31yB29W6tJFo2akwjPWcEqnCEdJvGexCL0KEQwVYv5BlowfhVz26hg==" data-cf-beacon='{"version":"2024.11.0","token":"3bf2bd78e087451ea36567e241d6aa66","r":1,"spa":2}' crossorigin="anonymous"></script>
3228 3229</body> 3230</html>
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.