2009-12-20 03:55:58 +00:00
<!DOCTYPE html>
< html >
< head >
< meta http-equiv = "content-type" content = "text/html;charset=UTF-8" / >
2009-12-24 01:24:55 +00:00
< title > CoffeeScript< / title >
2015-09-03 16:37:36 +00:00
< meta name = "viewport" content = "width=device-width, initial-scale=1" / >
2016-11-17 17:57:03 +00:00
< link rel = "canonical" href = "http://coffeescript.org" / >
2016-12-07 22:23:05 +00:00
< link rel = "apple-touch-icon" sizes = "180x180" href = "/apple-touch-icon.png" >
< link rel = "icon" type = "image/png" href = "/favicon-32x32.png" sizes = "32x32" >
< link rel = "icon" type = "image/png" href = "/favicon-16x16.png" sizes = "16x16" >
< link rel = "manifest" href = "/manifest.json" >
< link rel = "mask-icon" href = "/safari-pinned-tab.svg" color = "#5bbad5" >
< meta name = "theme-color" content = "#ffffff" >
< style >
body {
font-size: 14px;
line-height: 21px;
color: #333;
background: #f6f6f6 url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAADCAIAAABee8vuAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAB1JREFUeNpi/P79O8PgAyzMzMyD0FlMDIMSAAQYAC22AvZUamhbAAAAAElFTkSuQmCC);
font-family: "Helvetica Neue", "Lucida Grande", "Lucida Sans Unicode", Helvetica, Arial, sans-serif !important;
}
.container {
width: 950px;
margin: 0;
padding: 80px 0px 50px 50px;
clear: both;
}
p, li {
width: 625px;
}
a {
color: #191933;
}
h1, h2, h3, h4, h5, h6, b.header {
font-size: 18px;
color: #000;
margin-top: 40px;
margin-bottom: 15px;
text-shadow: #fff 0 1px 1px;
}
br.clear {
height: 0;
clear: both;
}
ul {
padding-left: 20px;
}
b.header {
display: block;
}
li {
margin-bottom: 10px;
}
table {
margin: 16px 0 0 13px; padding: 0;
width: 690px;
}
tr, td {
margin: 0; padding: 0;
}
td {
padding: 9px 15px 9px 0;
vertical-align: top;
}
table.definitions {
width: auto;
margin: 30px 0;
border-left: 5px solid rgba(0,0,0,0.2);;
}
table.definitions td {
text-align: center;
padding: 5px 20px;
}
code, pre, textarea {
font-family: Monaco, Consolas, "Lucida Console", monospace;
font-size: 12px;
line-height: 18px;
color: #155;
white-space: pre-wrap;
word-wrap: break-word;
}
p > code, li > code {
display: inline-block;
background: #fff;
border: 1px solid #dedede;
padding: 0px 0.2em;
}
pre {
border-left: 5px solid rgba(0,0,0,0.2);
padding: 3px 0 3px 12px;
font-size: 12px;
}
pre.no_bar {
border-left: 0;
margin-left: 0;
padding-left: 0;
}
.timestamp {
font-size: 11px;
font-weight: normal;
text-transform: uppercase;
}
div.anchor {
position: relative;
top: -90px;
margin: 0 0 -20px;
}
div.code {
position: relative;
background: #fff;
border: 1px solid #d8d8d8;
-webkit-box-shadow: 0px 0px 4px rgba(0,0,0,0.23);
-moz-box-shadow: 0px 0px 4px rgba(0,0,0,0.23);
box-shadow: 0px 0px 4px rgba(0,0,0,0.23);
zoom: 1;
}
div.code .minibutton {
text-transform: none;
position: absolute;
right: 8px; bottom: 8px;
}
div.code .load {
left: 8px; right: auto;
}
div.code pre, div.code textarea {
float: left;
width: 450px;
background: #fff;
border: 1px dotted #d0d0d0;
border-top-width: 0;
border-bottom-width: 0;
border-right-width: 0;
margin: 15px 3px;
padding: 0 0 26px 12px;
}
div.code pre:first-child {
border-left: 0;
}
#fadeout {
z-index: 50;
position: fixed;
left: 0; top: 0; right: 0;
height: 100px;
background: -webkit-gradient(linear, left top, left bottom, from(rgba(255, 255, 255, 255)), to(rgba(255, 255, 255, 0)));
background: -moz-linear-gradient(top, rgba(255, 255, 255, 255), rgba(255, 255, 255, 0));
}
#flybar {
position: fixed;
z-index: 100;
height: 50px;
min-width: 490px;
left: 40px; right: 40px; top: 25px;
background: #eee;
background: -webkit-gradient(linear, left top, left bottom, from(#f8f8f8), to(#dadada));
background: -moz-linear-gradient(top, #f8f8f8, #dadada);
border: 1px solid #aaa;
border-top: 1px solid #bbb;
border-bottom: 1px solid #888;
-webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px;
-webkit-box-shadow: 0 3px 5px rgba(0,0,0,0.1);
-moz-box-shadow: 0 3px 5px rgba(0,0,0,0.1);
box-shadow: 0 3px 5px rgba(0,0,0,0.1);
}
#logo {
display: block;
outline: none;
float: left;
width: 242px;
margin-left: 10px;
}
#logo svg {
width: 225px;
height: 40px;
margin: 5px 0 0 3px;
}
.navigation {
height: 50px;
font-size: 11px;
line-height: 50px;
text-transform: uppercase;
position: relative;
float: left;
padding: 0 20px;
border: 1px solid #aaa;
border-top: 0; border-bottom: 0; border-left-width: 0;
cursor: pointer;
}
.navigation.toc {
border-left-width: 1px;
}
.navigation:hover,
.navigation.active {
background: #eee;
background: -webkit-gradient(linear, left top, left bottom, from(#eee), to(#f8f8f8));
background: -moz-linear-gradient(top, #eee, #f8f8f8);
}
.navigation.active {
height: 51px;
color: #000;
background: -webkit-gradient(linear, left top, left bottom, from(#e5e5e5), to(#fff));
background: -moz-linear-gradient(top, #e5e5e5, #fff);
}
.navigation .button {
font-weight: bold;
}
.navigation .button::selection {
background: transparent;
}
.navigation .contents {
display: none;
position: absolute;
background: #fff;
opacity: 0.97;
top: 51px; left: 0;
padding: 5px 0;
margin-left: -1px;
border: 1px solid #aaa;
-webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px;
-webkit-box-shadow: 0 3px 5px rgba(0,0,0,0.2);
-moz-box-shadow: 0 3px 5px rgba(0,0,0,0.2);
box-shadow: 0 3px 5px rgba(0,0,0,0.2);
}
.navigation .contents a {
display: block;
width: 290px;
text-transform: none;
text-decoration: none;
height: 12px;
line-height: 12px;
padding: 4px 10px;
border: 1px solid transparent;
border-left: 0; border-right: 0;
}
.navigation .contents a:hover {
border-color: #ddd;
background: #eee;
}
.navigation.active .contents {
display: block;
}
.navigation .contents.menu {
z-index: 100;
border-top: 0;
-webkit-border-top-left-radius: 0; -moz-border-radius-topleft: 0; border-top-left-radius: 0;
-webkit-border-top-right-radius: 0; -moz-border-radius-topright: 0; border-top-right-radius: 0;
}
.navigation .contents.repl_wrapper {
padding: 0;
position: fixed;
width: auto; height: auto;
left: 40px; top: 90px; right: 40px; bottom: 30px;
background: -webkit-gradient(linear, left top, left bottom, from(#fafafa), to(#eaeaea));
}
.navigation .repl_bridge {
position: absolute;
height: 12px;
left: -1px; right: -1px;
bottom: -14px;
border: 1px solid #aaa;
z-index: 5;
background: #fff;
display: none;
border-top-color: #fff; border-bottom-color: #fff;
}
.navigation.active .repl_bridge {
display: block;
}
.navigation .code .minibutton {
top: 10px; right: 10px;
width: 40px;
text-transform: none;
}
.navigation .code a.minibutton.permalink {
top: 38px;
display: block;
}
.bookmark {
display: block;
width: 0; height: 0;
position: relative;
top: -90px;
}
.navigation .contents.repl_wrapper .code {
cursor: text;
-webkit-box-shadow: none; -moz-box-shadow: none; box-shadow: none;
background: #181a3a url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAOCAYAAAAmL5yKAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAACZJREFUeNpi/P//PyMDBYCJgULAAsQD64JRL1ApDP6PeoEyABBgAKOyBSJI2bJcAAAAAElFTkSuQmCC);
border: 2px solid #555;
padding: 0;
position: absolute;
top: 15px; left: 15px; right: 15px; bottom: 15px;
}
.repl_wrapper .screenshadow {
position: absolute;
width: 200px; height: 150px;
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/RSDkhb46+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/9Cs5QcHEI/3hGY03Tdup5B6oTvA2VkAmaydoRW7Kvrm24WmkO/bIyIAL8djkQ+sDa3YVr1Fu51ClvrsKhEBSDweoRG7+Mb8/udLWT4WOKAeR6RZ8s4BSFT3JrtZMB7tgfOPuBv9d/ztAfXdwndUN4mICMB/F2Ev+unmoQ1NfxzqW3IrK46ILS1g7eoqgV+N0d+6uheQpbey+neciABrjseia+C1r
}
.repl_wrapper .screenshadow.tl {
top: 0; left: 0;
background-position: 0 0;
}
.repl_wrapper .screenshadow.tr {
top: 0; right: 0;
background-position: -200px 0;
}
.repl_wrapper .screenshadow.bl {
bottom: 0; left: 0;
background-position: 0 -150px;
}
.repl_wrapper .screenshadow.br {
bottom: 0; right: 0;
background-position: -200px -150px;
}
#repl_source, #repl_results {
background: transparent;
outline: none;
margin: 5px 0 20px;
color: #def;
-webkit-tab-size: 2;
-moz-tab-size: 2;
-o-tab-size: 2;
tab-size: 2;
}
#repl_results, #repl_source_wrap {
width: auto; height: auto;
position: absolute;
margin-bottom: 0;
top: 10px; left: 10px; right: 10px; bottom: 15px;
}
#repl_results.error {
color: red
}
#repl_source_wrap {
margin-left: 5px;
width: 47%; right: 50%;
float: left;
}
#repl_source {
padding-left: 5px;
width: 100%;
height: 100%;
border: 0;
overflow-y: auto;
resize: none;
}
#repl_results_wrap {
white-space: pre;
}
#repl_results {
text-transform: none;
overflow-y: auto;
left: 50%;
border-left-color: #555;
}
/*----------------------------- Mini Buttons ---------------------------------*/
.minibutton {
cursor: pointer;
color: #333;
text-shadow: #eee 0 1px 1px;
font-weight: bold;
font-size: 11px;
line-height: 11px;
padding: 5px 10px 6px;
height: 11px;
text-align: center;
-webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px;
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);
border: 1px solid #b2b2b2; border-top-color: #c9c9c9; border-bottom-color: #9a9a9a;
background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAAWCAIAAACOpGH9AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAACxJREFUeNpifvr0KRMDAwPT////4RjE//fvHxiD+Mjsv3//4uSD2FD1AAEGAHv9O3d7p0bEAAAAAElFTkSuQmCC) repeat-x left top;
}
.minibutton:active {
border-color: #aaa;
box-shadow: 0 1px 2px #e4e4e4; -webkit-box-shadow: 0 1px 2px #e4e4e4; -moz-box-shadow: 0 1px 2px #e4e4e4;
}
.minibutton::selection {
background: transparent;
}
.minibutton ::-moz-selection {
background: transparent;
}
.minibutton.ok {
color: #fff;
background-image: url(data:image/gif;base64,R0lGODlhAQAYALMAADOtbj3BeyqVXDq6dTClZy2gYzKoazu+eDm1cyyaYDaycj7DfAAAAAAAAAAAAAAAACH5BAAAAAAALAAAAAABABgAAAQMcMlJZzgDKWBISUIEADs=);
border-color: #4ba47c; border-top-color: #53b388; border-bottom-color: #459671;
text-shadow: #aaa 0 -1px 0;
}
.minibutton.dark {
border: 0;
color: #fff;
box-shadow: none; -webkit-box-shadow: none; -moz-box-shadow: none;
background-image: url(data:image/gif;base64,R0lGODlhAQAYAMQAAFZWVn19fW1tbXd3d3FxcWtra1tbW3V1dWVlZWJiYlJSUnx8fF9fX3l5eWlpaXNzc35+fgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAAAAAAALAAAAAABABgAAAURICSOYrA0w/EQQuEgCWMASggAOw==);
text-shadow: none;
}
.minibutton.error {
opacity: 0.5;
color: #600;
cursor: not-allowed;
}
@media (max-width: 820px) {
.container {
width: auto;
padding: 1em;
}
p, li, table {
width: auto;
}
#fadeout {
display: none;
}
#flybar {
position: static;
height: auto;
min-width: 245px;
}
#logo {
float: none;
}
.navigation {
float: none;
border: none;
}
div.code pre, div.code textarea {
border-left: none;
border-top-width: 1px;
width: auto;
float: none;
margin: 5px;
padding: 10px 5px;
}
div.code pre:first-child {
border-top: none;
}
}
/* http://jmblog.github.com/color-themes-for-google-code-highlightjs */
/* Original code:; http://softwaremaniacs.org/media/soft/highlight/styles/tomorrow.css */
/* But forked for CoffeeScript */
.tomorrow-comment, pre .comment, pre .title {
color: #8e908c;
}
.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 {
color: #c82829;
}
.tomorrow-orange, pre .number, pre .preprocessor, pre .built_in, pre .params, pre .constant {
color: #000000;
}
.tomorrow-yellow, pre .class, pre .ruby .class .title, pre .css .rules .attribute {
color: #eab700;
}
.tomorrow-green, pre .string, pre .value, pre .inheritance, pre .header, pre .ruby .symbol, pre .xml .cdata {
color: #718c00;
}
.tomorrow-aqua, pre .css .hexcolor {
color: #3e999f;
}
.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 {
color: #21439C;
}
.tomorrow-purple, pre .keyword, pre .reserved, pre .javascript .function {
color: #FF5600;
}
pre .subst {
color: #A535AE;
}
pre .literal {
color: #A535AE;
}
pre .property {
color: #A535AE;
}
pre .class .title {
color: #21439C;
}
pre code {
display: block;
background: white;
color: #000000;
}
pre .coffeescript .javascript,
pre .javascript .xml,
pre .tex .formula,
pre .xml .javascript,
pre .xml .vbscript,
pre .xml .css,
pre .xml .cdata {
opacity: 0.5;
}
< / style >
2009-12-20 03:55:58 +00:00
< / head >
2010-12-13 02:41:04 +00:00
< body >
2010-02-21 21:36:34 +00:00
2010-02-21 19:30:21 +00:00
< div id = "fadeout" > < / div >
< div id = "flybar" >
2016-12-07 22:23:05 +00:00
< a id = "logo" href = "#top" > < svg xmlns = "http://www.w3.org/2000/svg" viewBox = "-22 347 566 100" >
< title >
CoffeeScript Logo
< / title >
< path fill = "#28334C" 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 0 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 .
< / svg >
< / a >
2010-03-08 11:34:07 +00:00
< div class = "navigation toc" >
2010-02-21 19:30:21 +00:00
< div class = "button" >
Table of Contents
< / div >
2010-12-13 02:41:04 +00:00
< div class = "contents menu" >
2010-11-22 00:53:31 +00:00
< a href = "#overview" > Overview< / a >
2011-12-18 18:41:33 +00:00
< a href = "#installation" > Installation< / a >
< a href = "#usage" > Usage< / a >
2013-02-25 08:12:22 +00:00
< a href = "#literate" > Literate CoffeeScript< / a >
2010-02-21 21:13:09 +00:00
< a href = "#language" > Language Reference< / a >
2010-12-22 03:24:24 +00:00
< a href = "#literals" > Literals: Functions, Objects and Arrays< / a >
2013-03-04 19:19:08 +00:00
< a href = "#lexical-scope" > Lexical Scoping and Variable Safety< / a >
2010-07-17 02:44:07 +00:00
< a href = "#conditionals" > If, Else, Unless, and Conditional Assignment< / a >
2016-12-01 06:28:00 +00:00
< a href = "#splats" > Splats…< / a >
2010-12-22 03:24:24 +00:00
< a href = "#loops" > Loops and Comprehensions< / a >
2010-12-04 17:52:51 +00:00
< a href = "#slices" > Array Slicing and Splicing< / a >
2010-02-21 19:30:21 +00:00
< a href = "#expressions" > Everything is an Expression< / a >
2010-12-22 03:24:24 +00:00
< a href = "#operators" > Operators and Aliases< / a >
2016-11-21 01:05:19 +00:00
< a href = "#existential-operator" > Existential Operator< / a >
2010-02-28 01:12:47 +00:00
< a href = "#classes" > Classes, Inheritance, and Super< / a >
2010-12-23 21:30:35 +00:00
< a href = "#destructuring" > Destructuring Assignment< / a >
2015-01-29 17:25:25 +00:00
< a href = "#fat-arrow" > Bound and Generator Functions< / a >
2010-02-21 19:30:21 +00:00
< a href = "#embedded" > Embedded JavaScript< / a >
2010-12-22 03:24:24 +00:00
< a href = "#switch" > Switch and Try/Catch< / a >
2010-02-21 19:30:21 +00:00
< a href = "#comparisons" > Chained Comparisons< / a >
2012-04-25 18:37:05 +00:00
< a href = "#strings" > String Interpolation, Block Strings, and Block Comments< / a >
2016-12-01 06:28:00 +00:00
< a href = "#tagged-template-literals" > Tagged Template Literals< / a >
2012-04-25 18:37:05 +00:00
< a href = "#regexes" > Block Regular Expressions< / a >
2016-09-22 20:19:43 +00:00
< a href = "#modules" > Modules< / a >
2010-02-21 23:37:52 +00:00
< a href = "#cake" > Cake, and Cakefiles< / a >
2013-03-04 19:19:08 +00:00
< a href = "#source-maps" > Source Maps< / a >
2010-02-25 05:26:59 +00:00
< a href = "#scripts" > "text/coffeescript" Script Tags< / a >
2011-08-05 03:45:38 +00:00
< a href = "#resources" > Books, Screencasts, Examples and Resources< / a >
2011-11-08 23:11:33 +00:00
< a href = "#changelog" > Change Log< / a >
2010-02-21 19:30:21 +00:00
< / div >
< / div >
2010-02-21 21:13:09 +00:00
< div class = "navigation try" >
< div class = "button" >
Try CoffeeScript
2010-12-22 02:35:43 +00:00
< div class = "repl_bridge" > < / div >
2010-02-21 21:13:09 +00:00
< / div >
< div class = "contents repl_wrapper" >
< div class = "code" >
2010-12-13 02:41:04 +00:00
< div class = "screenshadow tl" > < / div >
< div class = "screenshadow tr" > < / div >
< div class = "screenshadow bl" > < / div >
< div class = "screenshadow br" > < / div >
< div id = "repl_source_wrap" >
2011-02-09 23:52:35 +00:00
< textarea id = "repl_source" rows = "100" spellcheck = "false" > alert "Hello CoffeeScript!"< / textarea >
2010-12-13 02:41:04 +00:00
< / div >
2010-12-24 19:02:10 +00:00
< div id = "repl_results_wrap" > < pre id = "repl_results" > < / pre > < / div >
2011-02-10 16:28:02 +00:00
< div class = "minibutton dark run" title = "Ctrl-Enter" > Run< / div >
2011-09-19 01:53:08 +00:00
< a class = "minibutton permalink" id = "repl_permalink" > Link< / a >
2010-02-21 21:13:09 +00:00
< br class = "clear" / >
< / div >
< / div >
< / div >
2010-03-08 11:34:07 +00:00
< div class = "navigation annotated" >
< div class = "button" >
Annotated Source
< / div >
2010-12-13 02:41:04 +00:00
< div class = "contents menu" >
2016-12-01 06:28:00 +00:00
< a href = "v1/annotated-source/grammar.html" > Grammar Rules — src/grammar< / a >
< a href = "v1/annotated-source/lexer.html" > Lexing Tokens — src/lexer< / a >
< a href = "v1/annotated-source/rewriter.html" > The Rewriter — src/rewriter< / a >
< a href = "v1/annotated-source/nodes.html" > The Syntax Tree — src/nodes< / a >
< a href = "v1/annotated-source/scope.html" > Lexical Scope — src/scope< / a >
< a href = "v1/annotated-source/helpers.html" > Helpers & Utility Functions — src/helpers< / a >
< a href = "v1/annotated-source/coffee-script.html" > The CoffeeScript Module — src/coffee-script< / a >
< a href = "v1/annotated-source/cake.html" > Cake & Cakefiles — src/cake< / a >
< a href = "v1/annotated-source/command.html" > “coffee” Command-Line Utility — src/command< / a >
< a href = "v1/annotated-source/optparse.html" > Option Parsing — src/optparse< / a >
< a href = "v1/annotated-source/repl.html" > Interactive REPL — src/repl< / a >
< a href = "v1/annotated-source/sourcemap.html" > Source Maps — src/sourcemap< / a >
2010-03-08 11:34:07 +00:00
< / div >
< / div >
2010-02-21 19:30:21 +00:00
< / div >
2009-12-20 03:55:58 +00:00
< div class = "container" >
2010-02-21 21:13:09 +00:00
< span class = "bookmark" id = "top" > < / span >
2009-12-20 03:55:58 +00:00
< p >
2013-02-25 08:12:22 +00:00
< b > CoffeeScript is a little language that compiles into JavaScript.< / b >
Underneath that awkward Java-esque patina, JavaScript has always had
a gorgeous heart. CoffeeScript is an attempt to expose
2010-11-21 17:38:27 +00:00
the good parts of JavaScript in a simple way.
2009-12-21 16:41:45 +00:00
< / p >
< p >
2016-12-01 06:28:00 +00:00
The golden rule of CoffeeScript is: < i > “It’ s just JavaScript”< / i > . The code
2010-11-21 17:38:27 +00:00
compiles one-to-one into the equivalent JS, and there is
2010-11-22 02:12:59 +00:00
no interpretation at runtime. You can use any existing JavaScript library
2013-01-28 10:06:07 +00:00
seamlessly from CoffeeScript (and vice-versa). The compiled output is
2016-12-01 06:28:00 +00:00
readable, pretty-printed, and tends to run as fast or faster than the
equivalent handwritten JavaScript.
< / p >
< 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 > .
2009-12-21 16:41:45 +00:00
< / p >
2010-01-05 03:19:45 +00:00
2010-01-05 00:15:24 +00:00
< p >
< b > Latest Version:< / b >
2016-12-07 05:15:04 +00:00
< a href = "http://github.com/jashkenas/coffeescript/tarball/1.12.1" > 1.12.1< / a >
2010-01-05 00:15:24 +00:00
< / p >
2009-12-21 16:41:45 +00:00
2014-08-23 14:08:39 +00:00
< pre > npm install -g coffee-script< / pre >
2013-03-04 19:19:08 +00:00
2010-02-21 19:30:21 +00:00
< h2 >
< span id = "overview" class = "bookmark" > < / span >
2010-11-21 17:38:27 +00:00
Overview
2010-02-21 19:30:21 +00:00
< / h2 >
2009-12-24 00:42:18 +00:00
< p > < i > CoffeeScript on the left, compiled JavaScript output on the right.< / i > < / p >
2014-01-28 03:30:06 +00:00
< div class = 'code' > < pre > < code > < span class = "comment" > # Assignment:< / span >
number = < span class = "number" > 42< / span >
opposite = < span class = "literal" > true< / span >
2009-12-24 00:42:18 +00:00
2014-01-28 03:30:06 +00:00
< span class = "comment" > # Conditions:< / span >
2016-09-22 20:19:43 +00:00
number = < span class = "number" > -42< / span > < span class = "keyword" > if< / span > opposite
2009-12-24 00:42:18 +00:00
2014-01-28 03:30:06 +00:00
< span class = "comment" > # Functions:< / span >
< span class = "function" > < span class = "title" > square< / span > = < span class = "params" > (x)< / span > -> < / span > x * x
2009-12-24 00:42:18 +00:00
2014-01-28 03:30:06 +00:00
< span class = "comment" > # Arrays:< / span >
list = [< 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 > ]
2009-12-24 00:42:18 +00:00
2014-01-28 03:30:06 +00:00
< span class = "comment" > # Objects:< / span >
2014-01-28 01:56:29 +00:00
math =
2016-09-22 20:19:43 +00:00
root: Math.sqrt
square: square
cube: < span class = "function" > < span class = "params" > (x)< / span > -> < / span > x * square x
2009-12-24 00:42:18 +00:00
2014-01-28 03:30:06 +00:00
< span class = "comment" > # Splats:< / span >
< span class = "function" > < span class = "title" > race< / span > = < span class = "params" > (winner, runners...)< / span > -> < / span >
< span class = "built_in" > print< / span > winner, runners
2010-01-05 05:34:18 +00:00
2014-01-28 03:30:06 +00:00
< span class = "comment" > # Existence:< / span >
alert < span class = "string" > "I knew it!"< / span > < span class = "keyword" > if< / span > elvis?
2010-01-05 05:34:18 +00:00
2014-01-28 03:30:06 +00:00
< span class = "comment" > # Array comprehensions:< / span >
cubes = (math.cube num < span class = "keyword" > for< / span > num < span class = "keyword" > in< / span > list)
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > cubes, list, math, num, number, opposite, race, square,
2015-02-18 20:43:33 +00:00
slice = [].slice;
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
number = < span class = "number" > 42< / span > ;
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
opposite = < span class = "literal" > true< / span > ;
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
< span class = "keyword" > if< / span > (opposite) {
2016-09-22 20:19:43 +00:00
number = < span class = "number" > -42< / span > ;
2012-04-10 18:57:45 +00:00
}
2011-09-21 03:42:19 +00:00
2016-09-22 20:19:43 +00:00
square = < span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > x< / span > ) < / span > {
2014-01-28 03:30:06 +00:00
< span class = "keyword" > return< / span > x * x;
2010-02-25 22:18:28 +00:00
};
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
list = [< 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 > ];
2011-09-21 03:42:19 +00:00
2014-01-28 01:56:29 +00:00
math = {
2016-11-21 01:05:19 +00:00
< span class = "attr" > root< / span > : < span class = "built_in" > Math< / span > .sqrt,
< span class = "attr" > square< / span > : square,
< span class = "attr" > cube< / span > : < span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > x< / span > ) < / span > {
2014-01-28 03:30:06 +00:00
< span class = "keyword" > return< / span > x * square(x);
2010-02-25 22:18:28 +00:00
}
};
2011-09-21 03:42:19 +00:00
2016-09-22 20:19:43 +00:00
race = < span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > < / span > ) < / span > {
2014-01-28 03:30:06 +00:00
< span class = "keyword" > var< / span > runners, winner;
2015-02-18 20:43:33 +00:00
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 > ) : [];
2014-01-28 03:30:06 +00:00
< span class = "keyword" > return< / span > print(winner, runners);
2010-02-25 22:18:28 +00:00
};
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
< span class = "keyword" > if< / span > (< span class = "keyword" > typeof< / span > elvis !== < span class = "string" > "undefined"< / span > & & elvis !== < span class = "literal" > null< / span > ) {
alert(< span class = "string" > "I knew it!"< / span > );
2012-04-10 18:57:45 +00:00
}
2011-09-21 03:42:19 +00:00
2016-09-22 20:19:43 +00:00
cubes = (< span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > < / span > ) < / span > {
2015-02-18 20:43:33 +00:00
< span class = "keyword" > var< / span > i, len, results;
results = [];
< span class = "keyword" > for< / span > (i = < span class = "number" > 0< / span > , len = list.length; i < len; i++) {
num = list[i];
results.push(math.cube(num));
2010-11-21 17:38:27 +00:00
}
2015-02-18 20:43:33 +00:00
< span class = "keyword" > return< / span > results;
2010-12-23 20:41:42 +00:00
})();
2016-12-01 06:28:00 +00:00
< / code > < / pre > < 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 > < div class = "minibutton ok" onclick = "javascript: var cubes , list , math , num , number , opposite , race , square ,
2015-02-18 20:43:33 +00:00
slice = [].slice;
2011-09-21 03:42:19 +00:00
2010-02-25 22:18:28 +00:00
number = 42;
2011-09-21 03:42:19 +00:00
2010-06-28 04:19:58 +00:00
opposite = true;
2011-09-21 03:42:19 +00:00
2012-04-10 18:57:45 +00:00
if (opposite) {
number = -42;
}
2011-09-21 03:42:19 +00:00
2010-05-15 05:18:05 +00:00
square = function(x) {
2010-02-25 22:18:28 +00:00
return x * x;
};
2011-09-21 03:42:19 +00:00
2010-02-25 22:18:28 +00:00
list = [1, 2, 3, 4, 5];
2011-09-21 03:42:19 +00:00
2010-02-25 22:18:28 +00:00
math = {
root: Math.sqrt,
square: square,
2010-05-15 05:18:05 +00:00
cube: function(x) {
2010-02-25 22:18:28 +00:00
return x * square(x);
}
};
2011-09-21 03:42:19 +00:00
2010-11-21 17:38:27 +00:00
race = function() {
var runners, winner;
2015-02-18 20:43:33 +00:00
winner = arguments[0], runners = 2 < = arguments.length ? slice.call(arguments, 1) : [];
2010-02-25 22:18:28 +00:00
return print(winner, runners);
};
2011-09-21 03:42:19 +00:00
2016-12-01 06:28:00 +00:00
if (typeof elvis !== " undefined" & & elvis !== null) {
alert(" I knew it!" );
2012-04-10 18:57:45 +00:00
}
2011-09-21 03:42:19 +00:00
2010-11-22 02:12:59 +00:00
cubes = (function() {
2015-02-18 20:43:33 +00:00
var i, len, results;
results = [];
for (i = 0, len = list.length; i < len ; i + + ) {
num = list[i];
results.push(math.cube(num));
2010-11-21 17:38:27 +00:00
}
2015-02-18 20:43:33 +00:00
return results;
2011-09-21 03:42:19 +00:00
})();
2016-12-01 06:28:00 +00:00
;alert(cubes);">run: cubes< / div > < br class = 'clear' / > < / div >
2010-01-05 04:26:27 +00:00
2010-02-21 19:30:21 +00:00
< h2 >
< span id = "installation" class = "bookmark" > < / span >
2011-12-18 18:41:33 +00:00
Installation
2010-02-21 19:30:21 +00:00
< / h2 >
2009-12-25 08:16:56 +00:00
2009-12-24 08:12:07 +00:00
< p >
2010-12-23 21:30:35 +00:00
The CoffeeScript compiler is itself
2016-11-16 05:37:41 +00:00
< a href = "v1/annotated-source/grammar.html" > written in CoffeeScript< / a > ,
2010-12-23 21:44:12 +00:00
using the < a href = "http://jison.org" > Jison parser generator< / a > . The
2015-09-03 16:37:36 +00:00
command-line version of < code > coffee< / code > is available as a
2010-12-23 21:44:12 +00:00
< a href = "http://nodejs.org/" > Node.js< / a > utility. The
2016-11-28 04:47:54 +00:00
< a href = "v1/browser-compiler/coffee-script.js" > core compiler< / a > however, does not
2010-12-23 21:44:12 +00:00
depend on Node, and can be run in any JavaScript environment, or in the
2016-12-01 06:28:00 +00:00
browser (see “Try CoffeeScript”, above).
2010-02-21 22:30:41 +00:00
< / p >
2010-02-21 23:37:52 +00:00
2010-02-21 22:30:41 +00:00
< p >
2010-12-18 16:54:40 +00:00
To install, first make sure you have a working copy of the latest stable version of
2015-01-29 17:20:46 +00:00
< a href = "http://nodejs.org/" > Node.js< / a > . You can then install CoffeeScript globally
with < a href = "http://npmjs.org" > npm< / a > :
2010-06-28 05:06:53 +00:00
< / p >
2010-07-17 14:28:06 +00:00
2010-06-28 05:06:53 +00:00
< pre >
2011-05-01 16:08:15 +00:00
npm install -g coffee-script< / pre >
< p >
2014-08-23 14:08:39 +00:00
When you need CoffeeScript as a dependency, install it locally:
2011-05-01 16:08:15 +00:00
< / p >
2013-01-28 10:06:07 +00:00
2014-08-23 14:08:39 +00:00
< pre >
npm install --save coffee-script< / pre >
2011-05-01 16:08:15 +00:00
< p >
2016-12-01 06:28:00 +00:00
If you’ d prefer to install the latest < b > master< / b > version of CoffeeScript, you
2011-05-01 16:08:15 +00:00
can clone the CoffeeScript
2014-05-08 19:07:42 +00:00
< a href = "http://github.com/jashkenas/coffeescript" > source repository< / a >
2011-05-01 16:08:15 +00:00
from GitHub, or download
2014-05-08 19:07:42 +00:00
< a href = "http://github.com/jashkenas/coffeescript/tarball/master" > the source< / a > directly.
2014-08-23 14:08:39 +00:00
To install the latest master CoffeeScript compiler with npm:
2012-10-08 14:08:23 +00:00
< / p >
< pre >
2014-08-23 14:08:39 +00:00
npm install -g jashkenas/coffeescript< / pre >
2012-10-08 14:08:23 +00:00
< p >
2016-12-01 06:28:00 +00:00
Or, if you want to install to < code > /usr/local< / code > , and don’ t want to use
2015-09-03 16:37:36 +00:00
npm to manage it, open the < code > coffee-script< / code > directory and run:
2011-05-01 16:08:15 +00:00
< / p >
2010-11-22 00:53:31 +00:00
< pre >
sudo bin/cake install< / pre >
2010-11-22 02:12:59 +00:00
2011-12-18 18:41:33 +00:00
< h2 >
< span id = "usage" class = "bookmark" > < / span >
Usage
< / h2 >
2010-06-28 05:06:53 +00:00
< p >
2015-09-03 16:37:36 +00:00
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
2010-11-22 00:53:31 +00:00
following options:
2009-12-24 00:42:18 +00:00
< / p >
< table >
< tr >
2010-02-27 05:38:04 +00:00
< td > < code > -c, --compile< / code > < / td >
2009-12-25 08:16:56 +00:00
< td >
2015-09-03 16:37:36 +00:00
Compile a < code > .coffee< / code > script into a < code > .js< / code > JavaScript file
2010-02-27 05:38:04 +00:00
of the same name.
2009-12-25 08:16:56 +00:00
< / td >
< / tr >
2013-03-04 19:19:08 +00:00
< tr >
< td > < code > -m, --map< / code > < / td >
< td >
Generate source maps alongside the compiled JavaScript files. Adds
2015-09-03 16:37:36 +00:00
< code > sourceMappingURL< / code > directives to the JavaScript as well.
2013-03-04 19:19:08 +00:00
< / td >
< / tr >
2016-09-22 20:19:43 +00:00
< tr >
< td > < code > -M, --inline-map< / code > < / td >
< td >
Just like < code > --map< / code > , but include the source map directly in
the compiled JavaScript files, rather than in a separate file.
< / td >
< / tr >
2009-12-25 08:16:56 +00:00
< tr >
2010-02-27 05:38:04 +00:00
< td width = "25%" > < code > -i, --interactive< / code > < / td >
2009-12-25 08:16:56 +00:00
< td >
2010-03-08 11:34:07 +00:00
Launch an interactive CoffeeScript session to try short snippets.
2015-09-03 16:37:36 +00:00
Identical to calling < code > coffee< / code > with no arguments.
2009-12-25 08:16:56 +00:00
< / td >
< / tr >
< tr >
< td > < code > -o, --output [DIR]< / code > < / td >
2009-12-24 00:42:18 +00:00
< td >
Write out all compiled JavaScript files into the specified directory.
2015-09-03 16:37:36 +00:00
Use in conjunction with < code > --compile< / code > or < code > --watch< / code > .
2009-12-24 00:42:18 +00:00
< / td >
< / tr >
2010-12-23 06:43:13 +00:00
< tr >
2011-05-01 16:08:15 +00:00
< td > < code > -j, --join [FILE]< / code > < / td >
2010-12-23 06:43:13 +00:00
< td >
Before compiling, concatenate all scripts together in the order they
2016-09-22 20:19:43 +00:00
were passed, and write them into the specified file. (Deprecated.)
2010-12-23 06:43:13 +00:00
< / td >
< / tr >
2009-12-24 00:42:18 +00:00
< tr >
< td > < code > -w, --watch< / code > < / td >
< td >
2011-11-08 23:12:58 +00:00
Watch files for changes, rerunning the specified command when any
file is updated.
2009-12-24 00:42:18 +00:00
< / td >
< / tr >
< tr >
< td > < code > -p, --print< / code > < / td >
< td >
Instead of writing out the JavaScript as a file, print it
directly to < b > stdout< / b > .
< / td >
< / tr >
2010-02-24 23:27:10 +00:00
< tr >
< td > < code > -s, --stdio< / code > < / td >
< td >
Pipe in CoffeeScript to STDIN and get back JavaScript over STDOUT.
Good for use with processes written in other languages. An example:< br / >
2015-09-03 16:37:36 +00:00
< code > cat src/cake.coffee | coffee -sc< / code >
2010-02-24 23:27:10 +00:00
< / td >
< / tr >
2013-06-02 05:37:45 +00:00
< tr >
< td > < code > -l, --literate< / code > < / td >
< td >
Parses the code as Literate CoffeeScript. You only need to specify
this when passing in code directly over < b > stdio< / b > , or using some sort
of extension-less file name.
< / td >
< / tr >
2009-12-24 00:42:18 +00:00
< tr >
< td > < code > -e, --eval< / code > < / td >
< td >
Compile and print a little snippet of CoffeeScript directly from the
2015-09-03 16:37:36 +00:00
command line. For example:< br / > < code > coffee -e "console.log num for num in [10..1]"< / code >
2009-12-24 00:42:18 +00:00
< / td >
< / tr >
2016-09-22 20:19:43 +00:00
< tr >
< td > < code > -r, --require [MODULE]< / code > < / td >
< td >
< code > require()< / code > the given module before starting the REPL or
evaluating the code given with the < code > --eval< / code > flag.
< / td >
< / tr >
2009-12-25 01:37:24 +00:00
< tr >
2010-10-13 19:09:56 +00:00
< td > < code > -b, --bare< / code > < / td >
2009-12-25 01:37:24 +00:00
< td >
2013-01-28 10:06:07 +00:00
Compile the JavaScript without the
2013-03-04 19:19:08 +00:00
< a href = "#lexical-scope" > top-level function safety wrapper< / a > .
2010-01-10 22:14:20 +00:00
< / td >
< / tr >
< tr >
2010-02-21 22:30:41 +00:00
< td > < code > -t, --tokens< / code > < / td >
2010-01-10 22:14:20 +00:00
< td >
2010-02-21 22:30:41 +00:00
Instead of parsing the CoffeeScript, just lex it, and print out the
2016-09-22 20:19:43 +00:00
token stream: < code > [IDENTIFIER square] [= =] [PARAM_START (]< / code > ...
2009-12-25 01:37:24 +00:00
< / td >
< / tr >
2009-12-24 07:01:39 +00:00
< tr >
2010-02-25 23:43:33 +00:00
< td > < code > -n, --nodes< / code > < / td >
2009-12-24 07:01:39 +00:00
< td >
2010-02-21 22:30:41 +00:00
Instead of compiling the CoffeeScript, just lex and parse it, and print
out the parse tree:
2010-02-22 22:14:51 +00:00
< pre class = "no_bar" >
2016-09-22 20:19:43 +00:00
Block
2010-02-22 22:14:51 +00:00
Assign
2016-09-22 20:19:43 +00:00
Value IdentifierLiteral: square
Code
Param IdentifierLiteral: x
Block
Op *
Value IdentifierLiteral: x
Value IdentifierLiteral: x< / pre >
2009-12-24 07:01:39 +00:00
< / td >
< / tr >
2010-12-18 16:54:40 +00:00
< tr >
< td > < code > --nodejs< / code > < / td >
< td >
2015-09-03 16:37:36 +00:00
The < code > node< / code > executable has some useful options you can set,
such as< br / > < 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.
2010-12-18 16:54:40 +00:00
< / td >
< / tr >
2016-09-22 20:19:43 +00:00
< tr >
< td > < code > --no-header< / code > < / td >
< td >
2016-12-01 06:28:00 +00:00
Suppress the “Generated by CoffeeScript” header.
2016-09-22 20:19:43 +00:00
< / td >
< / tr >
2009-12-24 00:42:18 +00:00
< / table >
< p >
< b > Examples:< / b >
< / p >
2010-12-23 21:44:12 +00:00
2010-12-23 21:30:35 +00:00
< ul >
< li >
2015-09-03 16:37:36 +00:00
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 / >
< code > coffee --compile --output lib/ src/< / code >
2010-12-23 21:30:35 +00:00
< / li >
< li >
Watch a file for changes, and recompile it every time the file is saved:< br / >
2015-09-03 16:37:36 +00:00
< code > coffee --watch --compile experimental.coffee< / code >
2010-12-23 21:30:35 +00:00
< / li >
< li >
Concatenate a list of files into a single script:< br / >
2015-09-03 16:37:36 +00:00
< code > coffee --join project.js --compile src/*.coffee< / code >
2010-12-23 21:30:35 +00:00
< / li >
< li >
Print out the compiled JS from a one-liner:< br / >
2015-09-03 16:37:36 +00:00
< code > coffee -bpe "alert i for i in [0..10]"< / code >
2010-12-23 21:30:35 +00:00
< / li >
2011-11-08 23:12:58 +00:00
< li >
All together now, watch and recompile an entire project as you work on it:< br / >
2015-09-03 16:37:36 +00:00
< code > coffee -o lib/ -cw src/< / code >
2011-11-08 23:12:58 +00:00
< / li >
2010-12-23 21:30:35 +00:00
< li >
2015-09-03 16:37:36 +00:00
Start the CoffeeScript REPL (< code > Ctrl-D< / code > to exit, < code > Ctrl-V< / code > for multi-line):< br / >
< code > coffee< / code >
2010-12-23 21:30:35 +00:00
< / li >
< / ul >
2009-12-24 00:42:18 +00:00
2013-02-25 08:12:22 +00:00
< h2 >
< span id = "literate" class = "bookmark" > < / span >
Literate CoffeeScript
< / h2 >
< p >
Besides being used as an ordinary programming language, CoffeeScript may
2016-12-01 06:28:00 +00:00
also be written in “literate” mode. If you name your file with a
< code > .litcoffee< / code > extension, you can write it as a Markdown document —
2013-02-25 09:29:42 +00:00
a document that also happens to be executable CoffeeScript code. The compiler
2016-12-01 06:28:00 +00:00
will treat any indented blocks (Markdown’ s way of indicating source code)
2013-02-25 09:29:42 +00:00
as code, and ignore the rest as comments.
2013-02-25 08:12:22 +00:00
< / p >
< 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 > ,
2014-05-08 19:07:42 +00:00
< a href = "https://raw.github.com/jashkenas/coffeescript/master/src/scope.litcoffee" > raw< / a > ,
2013-02-25 08:12:22 +00:00
and < a href = "http://cl.ly/LxEu" > properly highlighted in a text editor< / a > .
< / p >
< p >
2016-12-01 06:28:00 +00:00
I’ m fairly excited about this direction for the language, and am looking
2013-02-25 08:12:22 +00:00
forward to writing (and more importantly, reading) more programs in this style.
2013-03-04 19:19:08 +00:00
More information about Literate CoffeeScript, including an
< a href = "https://github.com/jashkenas/journo" > example program< / a > ,
are < a href = "http://ashkenas.com/literate-coffeescript" > available in this blog post< / a > .
2013-02-25 08:12:22 +00:00
< / p >
2010-02-21 19:30:21 +00:00
< h2 >
2010-02-21 21:13:09 +00:00
< span id = "language" class = "bookmark" > < / span >
Language Reference
2010-02-21 19:30:21 +00:00
< / h2 >
2010-02-13 21:23:03 +00:00
2009-12-21 16:41:45 +00:00
< p >
2009-12-24 00:42:18 +00:00
< i >
2009-12-24 17:56:44 +00:00
This reference is structured so that it can be read from top to bottom,
2009-12-24 00:42:18 +00:00
if you like. Later sections use ideas and syntax previously introduced.
2009-12-24 07:01:39 +00:00
Familiarity with JavaScript is assumed.
2009-12-24 00:42:18 +00:00
In all of the following examples, the source CoffeeScript is provided on
the left, and the direct compilation into JavaScript is on the right.
< / i >
2009-12-21 16:41:45 +00:00
< / p >
2010-02-21 23:37:52 +00:00
2010-02-21 22:30:41 +00:00
< p >
< i >
2010-12-23 21:44:12 +00:00
Many of the examples can be run (where it makes sense) by pressing the < b > run< / b >
2016-12-01 06:28:00 +00:00
button on the right, and can be loaded into the “Try CoffeeScript”
2010-12-23 21:44:12 +00:00
console by pressing the < b > load< / b > button on the left.
2010-02-21 22:30:41 +00:00
< / i >
2010-02-21 19:30:21 +00:00
< p >
2010-12-23 22:26:10 +00:00
First, the basics: CoffeeScript uses significant whitespace to delimit blocks of code.
2016-12-01 06:28:00 +00:00
You don’ t need to use semicolons < code > ;< / code > to terminate expressions,
2011-07-09 05:26:30 +00:00
ending the line will do just as well (although semicolons can still
be used to fit multiple expressions onto a single line).
2010-12-23 22:26:10 +00:00
Instead of using curly braces
2015-09-03 16:37:36 +00:00
< code > { }< / code > to surround blocks of code in < a href = "#literals" > functions< / a > ,
2009-12-25 08:16:56 +00:00
< a href = "#conditionals" > if-statements< / a > ,
2010-12-23 22:26:10 +00:00
< a href = "#switch" > switch< / a > , and < a href = "#try" > try/catch< / a > ,
2010-01-05 03:19:45 +00:00
use indentation.
2009-12-24 00:42:18 +00:00
< / p >
2010-01-27 04:23:59 +00:00
< p >
2016-12-01 06:28:00 +00:00
You don’ t need to use parentheses to invoke a function if you’ re passing
2010-12-23 22:26:10 +00:00
arguments. The implicit call wraps forward to the end of the line or block expression.< br / >
2015-09-03 16:37:36 +00:00
< code > console.log sys.inspect object< / code > → < code > console.log(sys.inspect(object));< / code >
2010-01-06 02:40:36 +00:00
< / p >
2010-02-21 19:30:21 +00:00
< p >
2010-12-22 03:24:24 +00:00
< span id = "literals" class = "bookmark" > < / span >
2010-11-22 00:53:31 +00:00
< b class = "header" > Functions< / b >
2010-12-23 22:26:10 +00:00
Functions are defined by an optional list of parameters in parentheses,
an arrow, and the function body. The empty function looks like this:
2015-09-03 16:37:36 +00:00
< code > ->< / code >
2009-12-24 00:42:18 +00:00
< / p >
2014-01-28 03:30:06 +00:00
< div class = 'code' > < pre > < code > < span class = "function" > < span class = "title" > square< / span > = < span class = "params" > (x)< / span > -> < / span > x * x
< span class = "function" > < span class = "title" > cube< / span > = < span class = "params" > (x)< / span > -> < / span > square(x) * x
2016-11-16 06:08:27 +00:00
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > cube, square;
square = < span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > x< / span > ) < / span > {
< span class = "keyword" > return< / span > x * x;
};
cube = < span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > x< / span > ) < / span > {
< span class = "keyword" > return< / span > square(x) * x;
};
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example2 = "square = (x) -> x * x\ncube = (x) -> square(x) * x\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example2);' > load< / div > < div class = "minibutton ok" onclick = "javascript: var cube , square ;
2016-11-16 06:08:27 +00:00
square = function(x) {
return x * x;
};
cube = function(x) {
return square(x) * x;
};
2016-12-01 06:28:00 +00:00
;alert(cube(5));">run: cube(5)< / div > < br class = 'clear' / > < / div >
2010-11-22 02:12:59 +00:00
< p >
2014-01-28 01:56:29 +00:00
Functions may also have default values for arguments, which will be used
2015-09-03 16:37:36 +00:00
if the incoming argument is missing (< code > null< / code > or < code > undefined< / code > ).
2010-11-22 02:12:59 +00:00
< / p >
2014-01-28 03:30:06 +00:00
< 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 >
< span class = "string" > "Filling the < span class = "subst" > #{container}< / span > with < span class = "subst" > #{liquid}< / span > ..."< / span >
2016-11-16 06:08:27 +00:00
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > fill;
fill = < span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > container, liquid< / span > ) < / span > {
< span class = "keyword" > if< / span > (liquid == < span class = "literal" > null< / span > ) {
liquid = < span class = "string" > "coffee"< / span > ;
}
< span class = "keyword" > return< / span > < span class = "string" > "Filling the "< / span > + container + < span class = "string" > " with "< / span > + liquid + < span class = "string" > "..."< / span > ;
};
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example3 = "fill = (container, liquid = \"coffee\") ->\n \"Filling the #{container} with #{liquid}...\"\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example3);' > load< / div > < div class = "minibutton ok" onclick = "javascript: var fill ;
2016-11-16 06:08:27 +00:00
fill = function(container, liquid) {
if (liquid == null) {
2016-12-01 06:28:00 +00:00
liquid = " coffee" ;
2016-11-16 06:08:27 +00:00
}
2016-12-01 06:28:00 +00:00
return " Filling the " + container + " with " + liquid + " ..." ;
2016-11-16 06:08:27 +00:00
};
2016-12-01 06:28:00 +00:00
;alert(fill(" cup" ));">run: fill("cup")< / div > < br class = 'clear' / > < / div >
2009-12-24 00:42:18 +00:00
2010-02-21 19:30:21 +00:00
< p >
< span id = "objects_and_arrays" class = "bookmark" > < / span >
2009-12-24 00:42:18 +00:00
< b class = "header" > Objects and Arrays< / b >
2010-12-23 22:26:10 +00:00
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 > .
2009-12-24 00:42:18 +00:00
< / p >
2014-01-28 03:30:06 +00:00
< 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 > ]
2010-07-29 04:51:35 +00:00
2016-09-22 20:19:43 +00:00
singers = {Jagger: < span class = "string" > "Rock"< / span > , Elvis: < span class = "string" > "Roll"< / span > }
2010-01-05 03:19:45 +00:00
2014-01-28 01:56:29 +00:00
bitlist = [
2014-01-28 03:30:06 +00:00
< 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 >
2010-01-05 03:19:45 +00:00
]
2010-08-03 05:13:31 +00:00
2014-01-28 01:56:29 +00:00
kids =
2016-09-22 20:19:43 +00:00
brother:
name: < span class = "string" > "Max"< / span >
age: < span class = "number" > 11< / span >
sister:
name: < span class = "string" > "Ida"< / span >
age: < span class = "number" > 9< / span >
2016-11-16 05:55:19 +00:00
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > bitlist, kids, singers, song;
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 > ];
singers = {
2016-11-21 01:05:19 +00:00
< span class = "attr" > Jagger< / span > : < span class = "string" > "Rock"< / span > ,
< span class = "attr" > Elvis< / span > : < span class = "string" > "Roll"< / span >
2016-11-16 05:55:19 +00:00
};
bitlist = [< 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 > ];
kids = {
2016-11-21 01:05:19 +00:00
< span class = "attr" > brother< / span > : {
< span class = "attr" > name< / span > : < span class = "string" > "Max"< / span > ,
< span class = "attr" > age< / span > : < span class = "number" > 11< / span >
2016-11-16 05:55:19 +00:00
},
2016-11-21 01:05:19 +00:00
< span class = "attr" > sister< / span > : {
< span class = "attr" > name< / span > : < span class = "string" > "Ida"< / span > ,
< span class = "attr" > age< / span > : < span class = "number" > 9< / span >
2016-11-16 05:55:19 +00:00
}
};
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example4 = "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 > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example4);' > load< / div > < div class = "minibutton ok" onclick = "javascript: var bitlist , kids , singers , song ;
2016-11-16 05:55:19 +00:00
2016-12-01 06:28:00 +00:00
song = [" do" , " re" , " mi" , " fa" , " so" ];
2016-11-16 05:55:19 +00:00
singers = {
2016-12-01 06:28:00 +00:00
Jagger: " Rock" ,
Elvis: " Roll"
2016-11-16 05:55:19 +00:00
};
bitlist = [1, 0, 1, 0, 0, 1, 1, 1, 0];
kids = {
brother: {
2016-12-01 06:28:00 +00:00
name: " Max" ,
2016-11-16 05:55:19 +00:00
age: 11
},
sister: {
2016-12-01 06:28:00 +00:00
name: " Ida" ,
2016-11-16 05:55:19 +00:00
age: 9
}
};
2016-12-01 06:28:00 +00:00
;alert(song.join(" … " ));">run: song.join(" … ")< / div > < br class = 'clear' / > < / div >
2010-06-28 05:00:53 +00:00
< p >
2016-12-01 06:28:00 +00:00
In JavaScript, you can’ t use reserved words, like < code > class< / code > , as properties
2010-11-22 00:53:31 +00:00
of an object, without quoting them as strings. CoffeeScript notices reserved words
2016-12-01 06:28:00 +00:00
used as keys in objects and quotes them for you, so you don’ t have to worry
2010-11-22 00:53:31 +00:00
about it (say, when using jQuery).
2010-06-28 05:00:53 +00:00
< / p >
2014-01-28 01:56:29 +00:00
< div class = 'code' > < pre > < code > $('.account').attr class: 'active'
2010-11-22 00:53:31 +00:00
2014-01-28 01:56:29 +00:00
log object.class
2014-01-28 03:30:06 +00:00
< / code > < / pre > < pre > < code > $(< span class = "string" > '.account'< / span > ).attr({
< span class = "string" > "class"< / span > : < span class = "string" > 'active'< / span >
2010-06-28 05:00:53 +00:00
});
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
log(object[< span class = "string" > "class"< / span > ]);
2015-09-03 16:37:36 +00:00
< / code > < / pre > < script > window . example5 = "$('.account').attr class: 'active'\n\nlog object.class\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example5);' > load< / div > < br class = 'clear' / > < / div >
2016-12-01 06:28:00 +00:00
< p >
CoffeeScript has a shortcut for creating objects when you want the key
to be set with a variable of the same name.
< / p >
< div class = 'code' > < pre > < code > name = < span class = "string" > "Michelangelo"< / span >
mask = < span class = "string" > "orange"< / span >
weapon = < span class = "string" > "nunchuks"< / span >
turtle = {name, mask, weapon}
output = < 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 >
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > mask, name, output, turtle, weapon;
name = < span class = "string" > "Michelangelo"< / span > ;
mask = < span class = "string" > "orange"< / span > ;
weapon = < span class = "string" > "nunchuks"< / span > ;
turtle = {
< span class = "attr" > name< / span > : name,
< span class = "attr" > mask< / span > : mask,
< span class = "attr" > weapon< / span > : weapon
};
output = 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 > ;
< / code > < / pre > < script > window . example6 = "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 > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example6);' > load< / div > < br class = 'clear' / > < / div >
2009-12-24 00:42:18 +00:00
2010-02-21 19:30:21 +00:00
< p >
2013-03-04 19:19:08 +00:00
< span id = "lexical-scope" class = "bookmark" > < / span >
2009-12-24 00:42:18 +00:00
< b class = "header" > Lexical Scoping and Variable Safety< / b >
The CoffeeScript compiler takes care to make sure that all of your variables
2016-12-01 06:28:00 +00:00
are properly declared within lexical scope — you never need to write
2015-09-03 16:37:36 +00:00
< code > var< / code > yourself.
2009-12-24 00:42:18 +00:00
< / p >
2014-01-28 03:30:06 +00:00
< div class = 'code' > < pre > < code > outer = < span class = "number" > 1< / span >
< span class = "function" > < span class = "title" > changeNumbers< / span > = -> < / span >
2016-09-22 20:19:43 +00:00
inner = < span class = "number" > -1< / span >
2014-01-28 03:30:06 +00:00
outer = < span class = "number" > 10< / span >
2016-09-22 20:19:43 +00:00
inner = changeNumbers()
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > changeNumbers, inner, outer;
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
outer = < span class = "number" > 1< / span > ;
2011-09-21 03:42:19 +00:00
2016-09-22 20:19:43 +00:00
changeNumbers = < span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > < / span > ) < / span > {
2014-01-28 03:30:06 +00:00
< span class = "keyword" > var< / span > inner;
2016-09-22 20:19:43 +00:00
inner = < span class = "number" > -1< / span > ;
2014-01-28 03:30:06 +00:00
< span class = "keyword" > return< / span > outer = < span class = "number" > 10< / span > ;
2009-12-21 16:41:45 +00:00
};
2011-09-21 03:42:19 +00:00
2014-01-28 01:56:29 +00:00
inner = changeNumbers();
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example7 = "outer = 1\nchangeNumbers = ->\n inner = -1\n outer = 10\ninner = changeNumbers()\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example7);' > load< / div > < div class = "minibutton ok" onclick = "javascript: var changeNumbers , inner , outer ;
2011-09-21 03:42:19 +00:00
2010-06-28 04:19:58 +00:00
outer = 1;
2011-09-21 03:42:19 +00:00
2010-06-28 04:19:58 +00:00
changeNumbers = function() {
var inner;
inner = -1;
2010-10-25 01:50:34 +00:00
return outer = 10;
2009-12-21 16:41:45 +00:00
};
2011-09-21 03:42:19 +00:00
inner = changeNumbers();
2016-12-01 06:28:00 +00:00
;alert(inner);">run: inner< / div > < br class = 'clear' / > < / div >
2009-12-24 00:42:18 +00:00
< p >
2010-08-05 03:14:34 +00:00
Notice how all of the variable declarations have been pushed up to
2010-01-05 03:19:45 +00:00
the top of the closest scope, the first time they appear.
2016-12-01 06:28:00 +00:00
< b > outer< / b > is not redeclared within the inner function, because it’ s
2010-07-23 16:34:54 +00:00
already in scope; < b > inner< / b > within the function, on the other hand,
2010-01-05 03:19:45 +00:00
should not be able to change the value of the external variable of the same name, and
therefore has a declaration of its own.
2009-12-24 00:42:18 +00:00
< / p >
2010-03-09 00:27:38 +00:00
< p >
2016-12-01 06:28:00 +00:00
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.
2010-03-09 00:27:38 +00:00
< / p >
2009-12-24 00:42:18 +00:00
< p >
2009-12-24 07:01:39 +00:00
Although suppressed within this documentation for clarity, all
2009-12-24 00:42:18 +00:00
CoffeeScript output is wrapped in an anonymous function:
2016-12-01 06:28:00 +00:00
< code > (function(){ … })();< / code > This safety wrapper, combined with the
2015-09-03 16:37:36 +00:00
automatic generation of the < code > var< / code > keyword, make it exceedingly difficult
2010-01-27 04:23:59 +00:00
to pollute the global namespace by accident.
2010-01-17 23:33:31 +00:00
< / p >
2010-01-27 04:23:59 +00:00
< p >
2016-12-01 06:28:00 +00:00
If you’ d like to create top-level variables for other scripts to use,
2016-09-22 20:19:43 +00:00
attach them as properties on < b > window< / b > ; attach them as properties on the
< b > exports< / b > object in CommonJS; or use an < a href = "#modules" > < code > export< / code >
statement< / a > . If you’ re targeting both CommonJS and the browser, the
< b > existential operator< / b > (covered below), gives you a
reliable way to figure out where to add them: < code > exports ? this< / code >
2009-12-24 00:42:18 +00:00
< / p >
2010-02-21 19:30:21 +00:00
< p >
< span id = "conditionals" class = "bookmark" > < / span >
2010-07-17 02:44:07 +00:00
< b class = "header" > If, Else, Unless, and Conditional Assignment< / b >
2009-12-29 02:02:40 +00:00
< b > If/else< / b > statements can be written without the use of parentheses and
2010-01-05 03:19:45 +00:00
curly brackets. As with functions and other block expressions,
2016-12-01 06:28:00 +00:00
multi-line conditionals are delimited by indentation. There’ s also a handy
2015-09-03 16:37:36 +00:00
postfix form, with the < code > if< / code > or < code > unless< / code > at the end.
2009-12-24 00:42:18 +00:00
< / p >
2009-12-24 09:38:32 +00:00
< p >
2010-07-17 02:44:07 +00:00
CoffeeScript can compile < b > if< / b > statements into JavaScript expressions,
using the ternary operator when possible, and closure wrapping otherwise. There
2016-12-01 06:28:00 +00:00
is no explicit ternary statement in CoffeeScript — you simply use
2010-11-22 00:53:31 +00:00
a regular < b > if< / b > statement on a single line.
2009-12-24 09:38:32 +00:00
< / p >
2014-01-28 03:30:06 +00:00
< div class = 'code' > < pre > < code > mood = greatlyImproved < span class = "keyword" > if< / span > singing
2009-12-21 16:41:45 +00:00
2014-01-28 03:30:06 +00:00
< span class = "keyword" > if< / span > happy < span class = "keyword" > and< / span > knowsIt
2010-06-28 04:19:58 +00:00
clapsHands()
chaChaCha()
2014-01-28 03:30:06 +00:00
< span class = "keyword" > else< / span >
2010-07-29 04:51:35 +00:00
showIt()
2009-12-21 16:41:45 +00:00
2014-01-28 03:30:06 +00:00
date = < span class = "keyword" > if< / span > friday < span class = "keyword" > then< / span > sue < span class = "keyword" > else< / span > jill
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > date, mood;
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
< span class = "keyword" > if< / span > (singing) {
2014-01-28 01:56:29 +00:00
mood = greatlyImproved;
2012-04-10 18:57:45 +00:00
}
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
< span class = "keyword" > if< / span > (happy & & knowsIt) {
2010-06-28 04:19:58 +00:00
clapsHands();
chaChaCha();
2014-01-28 03:30:06 +00:00
} < span class = "keyword" > else< / span > {
2010-07-29 04:51:35 +00:00
showIt();
2009-12-21 16:41:45 +00:00
}
2011-09-21 03:42:19 +00:00
2014-01-28 01:56:29 +00:00
date = friday ? sue : jill;
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example8 = "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 > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example8);' > load< / div > < br class = 'clear' / > < / div >
2009-12-24 00:42:18 +00:00
2010-02-21 19:30:21 +00:00
< p >
< span id = "splats" class = "bookmark" > < / span >
2016-12-01 06:28:00 +00:00
< b class = "header" > Splats…< / b >
2010-01-05 04:26:27 +00:00
The JavaScript < b > arguments object< / b > is a useful way to work with
functions that accept variable numbers of arguments. CoffeeScript provides
2015-09-03 16:37:36 +00:00
splats < code > ...< / code > , both for function definition as well as invocation,
2010-01-17 23:33:31 +00:00
making variable numbers of arguments a little bit more palatable.
2010-01-05 04:26:27 +00:00
< / p >
2014-01-28 03:30:06 +00:00
< div class = 'code' > < pre > < code > gold = silver = rest = < span class = "string" > "unknown"< / span >
2016-09-22 20:19:43 +00:00
< span class = "function" >
< span class = "title" > awardMedals< / span > = < span class = "params" > (first, second, others...)< / span > -> < / span >
2014-01-28 01:56:29 +00:00
gold = first
silver = second
rest = others
contenders = [
2014-01-28 03:30:06 +00:00
< 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 >
2010-01-05 04:26:27 +00:00
]
2010-06-28 04:19:58 +00:00
awardMedals contenders...
2010-01-05 04:26:27 +00:00
2014-01-28 03:30:06 +00:00
alert < span class = "string" > "Gold: "< / span > + gold
alert < span class = "string" > "Silver: "< / span > + silver
alert < span class = "string" > "The Field: "< / span > + rest
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > awardMedals, contenders, gold, rest, silver,
2015-02-18 20:43:33 +00:00
slice = [].slice;
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
gold = silver = rest = < span class = "string" > "unknown"< / span > ;
2011-09-21 03:42:19 +00:00
2016-09-22 20:19:43 +00:00
awardMedals = < span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > < / span > ) < / span > {
2014-01-28 03:30:06 +00:00
< span class = "keyword" > var< / span > first, others, second;
2015-02-18 20:43:33 +00:00
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 > ) : [];
2014-01-28 01:56:29 +00:00
gold = first;
silver = second;
2014-01-28 03:30:06 +00:00
< span class = "keyword" > return< / span > rest = others;
2010-01-05 04:26:27 +00:00
};
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
contenders = [< 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 > ];
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
awardMedals.apply(< span class = "literal" > null< / span > , contenders);
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
alert(< span class = "string" > "Gold: "< / span > + gold);
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
alert(< span class = "string" > "Silver: "< / span > + silver);
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
alert(< span class = "string" > "The Field: "< / span > + rest);
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example9 = "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 > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example9);' > load< / div > < div class = "minibutton ok" onclick = "javascript: var awardMedals , contenders , gold , rest , silver ,
2015-02-18 20:43:33 +00:00
slice = [].slice;
2011-09-21 03:42:19 +00:00
2016-12-01 06:28:00 +00:00
gold = silver = rest = " unknown" ;
2011-09-21 03:42:19 +00:00
2010-11-21 17:38:27 +00:00
awardMedals = function() {
var first, others, second;
2015-02-18 20:43:33 +00:00
first = arguments[0], second = arguments[1], others = 3 < = arguments.length ? slice.call(arguments, 2) : [];
2010-01-05 04:26:27 +00:00
gold = first;
silver = second;
2010-10-25 01:50:34 +00:00
return rest = others;
2010-01-05 04:26:27 +00:00
};
2011-09-21 03:42:19 +00:00
2016-12-01 06:28:00 +00:00
contenders = [" Michael Phelps" , " Liu Xiang" , " Yao Ming" , " Allyson Felix" , " Shawn Johnson" , " Roman Sebrle" , " Guo Jingjing" , " Tyson Gay" , " Asafa Powell" , " Usain Bolt" ];
2011-09-21 03:42:19 +00:00
2010-12-01 19:17:10 +00:00
awardMedals.apply(null, contenders);
2011-09-21 03:42:19 +00:00
2016-12-01 06:28:00 +00:00
alert(" Gold: " + gold);
2011-09-21 03:42:19 +00:00
2016-12-01 06:28:00 +00:00
alert(" Silver: " + silver);
2011-09-21 03:42:19 +00:00
2016-12-01 06:28:00 +00:00
alert(" The Field: " + rest);
;">run< / div > < br class = 'clear' / > < / div >
2010-01-05 04:26:27 +00:00
2010-02-21 19:30:21 +00:00
< p >
2010-12-22 03:24:24 +00:00
< span id = "loops" class = "bookmark" > < / span >
2010-12-24 19:02:10 +00:00
< b class = "header" > Loops and Comprehensions< / b >
2016-12-01 06:28:00 +00:00
Most of the loops you’ ll write in CoffeeScript will be < b > comprehensions< / b >
2010-12-23 06:43:13 +00:00
over arrays, objects, and ranges. Comprehensions replace (and compile into)
2010-12-22 03:24:24 +00:00
< b > for< / b > loops, with optional guard clauses and the value of the current array index.
2009-12-24 00:42:18 +00:00
Unlike for loops, array comprehensions are expressions, and can be returned
2011-01-30 22:10:50 +00:00
and assigned.
2009-12-24 00:42:18 +00:00
< / p >
2014-01-28 03:30:06 +00:00
< div class = 'code' > < pre > < code > < span class = "comment" > # Eat lunch.< / span >
eat 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 > ]
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
< span class = "comment" > # Fine five course dining.< / span >
courses = [< 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 > ]
menu i + < span class = "number" > 1< / span > , dish < span class = "keyword" > for< / span > dish, i < span class = "keyword" > in< / span > courses
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
< span class = "comment" > # Health conscious meal.< / span >
foods = [< span class = "string" > 'broccoli'< / span > , < span class = "string" > 'spinach'< / span > , < span class = "string" > 'chocolate'< / span > ]
eat 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 >
2015-02-18 20:43:33 +00:00
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > courses, dish, food, foods, i, j, k, l, len, len1, len2, ref;
2011-09-21 03:42:19 +00:00
2015-02-18 20:43:33 +00:00
ref = [< span class = "string" > 'toast'< / span > , < span class = "string" > 'cheese'< / span > , < span class = "string" > 'wine'< / span > ];
< span class = "keyword" > for< / span > (j = < span class = "number" > 0< / span > , len = ref.length; j < len; j++) {
food = ref[j];
2010-12-05 20:29:28 +00:00
eat(food);
2009-12-21 16:41:45 +00:00
}
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
courses = [< 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 > ];
2011-09-21 03:42:19 +00:00
2015-02-18 20:43:33 +00:00
< span class = "keyword" > for< / span > (i = k = < span class = "number" > 0< / span > , len1 = courses.length; k < len1; i = ++k) {
2014-01-28 01:56:29 +00:00
dish = courses[i];
2014-01-28 03:30:06 +00:00
menu(i + < span class = "number" > 1< / span > , dish);
2011-09-21 03:42:19 +00:00
}
2014-01-28 03:30:06 +00:00
foods = [< span class = "string" > 'broccoli'< / span > , < span class = "string" > 'spinach'< / span > , < span class = "string" > 'chocolate'< / span > ];
2011-09-21 03:42:19 +00:00
2015-02-18 20:43:33 +00:00
< span class = "keyword" > for< / span > (l = < span class = "number" > 0< / span > , len2 = foods.length; l < len2; l++) {
food = foods[l];
2014-01-28 03:30:06 +00:00
< span class = "keyword" > if< / span > (food !== < span class = "string" > 'chocolate'< / span > ) {
2012-04-10 18:57:45 +00:00
eat(food);
}
2011-09-21 03:42:19 +00:00
}
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example10 = "# 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 > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example10);' > load< / div > < br class = 'clear' / > < / div >
2009-12-27 04:46:31 +00:00
< p >
2011-01-30 22:10:50 +00:00
Comprehensions should be able to handle most places where you otherwise
2011-12-18 18:41:33 +00:00
would use a loop, < b > each< / b > /< b > forEach< / b > , < b > map< / b > , or < b > select< / b > /< b > filter< / b > , for example:
2015-09-03 16:37:36 +00:00
< code > shortNames = (name for name in list when name.length < 5)< / code > < br / >
2010-01-05 03:19:45 +00:00
If 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
2010-06-28 04:19:58 +00:00
end of your comprehension.
2010-01-10 20:52:23 +00:00
< / p >
2014-01-28 03:30:06 +00:00
< 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 > ])
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > countdown, num;
2011-09-21 03:42:19 +00:00
2016-09-22 20:19:43 +00:00
countdown = (< span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > < / span > ) < / span > {
2015-02-18 20:43:33 +00:00
< span class = "keyword" > var< / span > i, results;
results = [];
< span class = "keyword" > for< / span > (num = i = < span class = "number" > 10< / span > ; i > = < span class = "number" > 1< / span > ; num = --i) {
results.push(num);
2010-01-10 20:52:23 +00:00
}
2015-02-18 20:43:33 +00:00
< span class = "keyword" > return< / span > results;
2010-12-23 20:41:42 +00:00
})();
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example11 = "countdown = (num for num in [10..1])\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example11);' > load< / div > < div class = "minibutton ok" onclick = "javascript: var countdown , num ;
2011-09-21 03:42:19 +00:00
2010-11-22 02:12:59 +00:00
countdown = (function() {
2015-02-18 20:43:33 +00:00
var i, results;
results = [];
for (num = i = 10; i >= 1; num = --i) {
results.push(num);
2010-01-10 20:52:23 +00:00
}
2015-02-18 20:43:33 +00:00
return results;
2011-09-21 03:42:19 +00:00
})();
2016-12-01 06:28:00 +00:00
;alert(countdown);">run: countdown< / div > < br class = 'clear' / > < / div >
2010-12-05 20:29:28 +00:00
< p >
2010-12-12 00:44:11 +00:00
Note how because we are assigning the value of the comprehensions to a
variable in the example above, CoffeeScript is collecting the result of
2010-12-05 20:29:28 +00:00
each iteration into an array. Sometimes functions end with loops that are
2016-12-01 06:28:00 +00:00
intended to run only for their side-effects. Be careful that you’ re not
2010-12-05 20:29:28 +00:00
accidentally returning the results of the comprehension in these cases,
2016-12-01 06:28:00 +00:00
by adding a meaningful return value — like < code > true< / code > — or < code > null< / code > ,
2010-12-05 20:29:28 +00:00
to the bottom of your function.
< / p >
2010-12-18 16:54:40 +00:00
< p >
2010-12-22 01:43:13 +00:00
To step through a range comprehension in fixed-size chunks,
2015-09-03 16:37:36 +00:00
use < code > by< / code > , for example:< br / >
< code > evens = (x for x in [0..10] by 2)< / code >
2010-12-18 16:54:40 +00:00
< / p >
2015-05-14 09:23:10 +00:00
< p >
2016-12-01 06:28:00 +00:00
If you don’ t need the current iteration value you may omit it:< br / >
2015-09-03 16:37:36 +00:00
< code > browser.closeCurrentTab() for [0...count]< / code >
2015-05-14 09:23:10 +00:00
< / p >
2010-01-05 03:19:45 +00:00
< p >
2010-01-10 20:52:23 +00:00
Comprehensions can also be used to iterate over the keys and values in
2015-09-03 16:37:36 +00:00
an object. Use < code > of< / code > to signal comprehension over the properties of
2010-01-11 05:01:16 +00:00
an object instead of the values in an array.
2009-12-27 04:46:31 +00:00
< / p >
2016-09-22 20:19:43 +00:00
< div class = 'code' > < pre > < code > yearsOld = max: < span class = "number" > 10< / span > , ida: < span class = "number" > 9< / span > , tim: < span class = "number" > 11< / span >
2010-01-10 20:52:23 +00:00
2014-01-28 03:30:06 +00:00
ages = < span class = "keyword" > for< / span > child, age < span class = "keyword" > of< / span > yearsOld
< span class = "string" > "< span class = "subst" > #{child}< / span > is < span class = "subst" > #{age}< / span > "< / span >
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > age, ages, child, yearsOld;
2011-09-21 03:42:19 +00:00
2014-01-28 01:56:29 +00:00
yearsOld = {
2016-11-21 01:05:19 +00:00
< span class = "attr" > max< / span > : < span class = "number" > 10< / span > ,
< span class = "attr" > ida< / span > : < span class = "number" > 9< / span > ,
< span class = "attr" > tim< / span > : < span class = "number" > 11< / span >
2010-01-10 20:52:23 +00:00
};
2011-09-21 03:42:19 +00:00
2016-09-22 20:19:43 +00:00
ages = (< span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > < / span > ) < / span > {
2015-02-18 20:43:33 +00:00
< span class = "keyword" > var< / span > results;
results = [];
2014-01-28 03:30:06 +00:00
< span class = "keyword" > for< / span > (child < span class = "keyword" > in< / span > yearsOld) {
2014-01-28 01:56:29 +00:00
age = yearsOld[child];
2015-02-18 20:43:33 +00:00
results.push(child + < span class = "string" > " is "< / span > + age);
2010-07-17 14:28:06 +00:00
}
2015-02-18 20:43:33 +00:00
< span class = "keyword" > return< / span > results;
2010-12-23 20:41:42 +00:00
})();
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example12 = "yearsOld = max: 10, ida: 9, tim: 11\n\nages = for child, age of yearsOld\n \"#{child} is #{age}\"\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example12);' > load< / div > < div class = "minibutton ok" onclick = "javascript: var age , ages , child , yearsOld ;
2011-09-21 03:42:19 +00:00
2010-06-28 04:19:58 +00:00
yearsOld = {
2010-01-10 20:52:23 +00:00
max: 10,
ida: 9,
tim: 11
};
2011-09-21 03:42:19 +00:00
2010-12-23 20:41:42 +00:00
ages = (function() {
2015-02-18 20:43:33 +00:00
var results;
results = [];
2010-10-05 03:21:16 +00:00
for (child in yearsOld) {
age = yearsOld[child];
2016-12-01 06:28:00 +00:00
results.push(child + " is " + age);
2010-07-17 14:28:06 +00:00
}
2015-02-18 20:43:33 +00:00
return results;
2011-09-21 03:42:19 +00:00
})();
2016-12-01 06:28:00 +00:00
;alert(ages.join(" , " ));">run: ages.join(", ")< / div > < br class = 'clear' / > < / div >
2010-08-05 03:14:34 +00:00
< p >
2010-12-22 01:43:13 +00:00
If you would like to iterate over just the keys that are defined on the
2015-09-03 16:37:36 +00:00
object itself, by adding a < code > hasOwnProperty< / code >
2012-02-04 01:07:48 +00:00
check to avoid properties that may be inherited from the prototype, use< br / >
2015-09-03 16:37:36 +00:00
< code > for own key, value of object< / code >
2010-08-05 03:14:34 +00:00
< / p >
2016-12-01 06:28:00 +00:00
< p >
To iterate a generator function, use < code > from< / code > .
See < a href = "#generator-iteration" > Generator Functions< / a > .
2010-12-24 19:02:10 +00:00
< p >
The only low-level loop that CoffeeScript provides is the < b > while< / b > loop. The
main difference from JavaScript is that the < b > while< / b > loop can be used
as an expression, returning an array containing the result of each iteration
through the loop.
< / p >
2014-01-28 03:30:06 +00:00
< div class = 'code' > < pre > < code > < span class = "comment" > # Econ 101< / span >
< span class = "keyword" > if< / span > < span class = "keyword" > this< / span > .studyingEconomics
buy() < span class = "keyword" > while< / span > supply > demand
sell() < span class = "keyword" > until< / span > supply > demand
2010-12-24 19:02:10 +00:00
2014-01-28 03:30:06 +00:00
< span class = "comment" > # Nursery Rhyme< / span >
num = < span class = "number" > 6< / span >
lyrics = < span class = "keyword" > while< / span > num -= < span class = "number" > 1< / span >
< span class = "string" > "< span class = "subst" > #{num}< / span > little monkeys, jumping on the bed.
2014-01-28 01:56:29 +00:00
One fell out and bumped his head."< / span >
2014-01-28 03:30:06 +00:00
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > lyrics, num;
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
< span class = "keyword" > if< / span > (< span class = "keyword" > this< / span > .studyingEconomics) {
< span class = "keyword" > while< / span > (supply > demand) {
2010-12-24 19:02:10 +00:00
buy();
}
2014-01-28 03:30:06 +00:00
< span class = "keyword" > while< / span > (!(supply > demand)) {
2010-12-24 19:02:10 +00:00
sell();
}
}
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
num = < span class = "number" > 6< / span > ;
2011-09-21 03:42:19 +00:00
2016-09-22 20:19:43 +00:00
lyrics = (< span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > < / span > ) < / span > {
2015-02-18 20:43:33 +00:00
< span class = "keyword" > var< / span > results;
results = [];
2014-01-28 03:30:06 +00:00
< span class = "keyword" > while< / span > (num -= < span class = "number" > 1< / span > ) {
2015-02-18 20:43:33 +00:00
results.push(num + < span class = "string" > " little monkeys, jumping on the bed. One fell out and bumped his head."< / span > );
2010-12-24 19:02:10 +00:00
}
2015-02-18 20:43:33 +00:00
< span class = "keyword" > return< / span > results;
2010-12-24 19:02:10 +00:00
})();
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example13 = "# 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 > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example13);' > load< / div > < div class = "minibutton ok" onclick = "javascript: var lyrics , num ;
2011-09-21 03:42:19 +00:00
2010-12-24 19:02:10 +00:00
if (this.studyingEconomics) {
while (supply > demand) {
buy();
}
while (!(supply > demand)) {
sell();
}
}
2011-09-21 03:42:19 +00:00
2010-12-24 19:02:10 +00:00
num = 6;
2011-09-21 03:42:19 +00:00
2010-12-24 19:02:10 +00:00
lyrics = (function() {
2015-02-18 20:43:33 +00:00
var results;
results = [];
2010-12-24 19:02:10 +00:00
while (num -= 1) {
2016-12-01 06:28:00 +00:00
results.push(num + " little monkeys, jumping on the bed. One fell out and bumped his head." );
2010-12-24 19:02:10 +00:00
}
2015-02-18 20:43:33 +00:00
return results;
2011-09-21 03:42:19 +00:00
})();
2016-12-01 06:28:00 +00:00
;alert(lyrics.join(" \n" ));">run: lyrics.join("\n")< / div > < br class = 'clear' / > < / div >
2010-12-24 19:02:10 +00:00
< p >
2015-09-03 16:37:36 +00:00
For readability, the < b > until< / b > keyword is equivalent to < code > while not< / code > ,
and the < b > loop< / b > keyword is equivalent to < code > while true< / code > .
2010-12-24 19:02:10 +00:00
< / p >
2010-12-23 06:43:13 +00:00
< p >
2016-12-01 06:28:00 +00:00
When using a JavaScript loop to generate functions, it’ s common to insert
2010-12-24 17:22:27 +00:00
a closure wrapper in order to ensure that loop variables are closed over,
2016-12-01 06:28:00 +00:00
and all the generated functions don’ t just share the final values. CoffeeScript
2015-09-03 16:37:36 +00:00
provides the < code > do< / code > keyword, which immediately invokes a passed function,
2010-12-24 19:02:10 +00:00
forwarding any arguments.
2010-12-24 17:22:27 +00:00
< / p >
2014-01-28 03:30:06 +00:00
< div class = 'code' > < pre > < code > < span class = "keyword" > for< / span > filename < span class = "keyword" > in< / span > list
2016-09-22 20:19:43 +00:00
< span class = "keyword" > do< / span > (filename) ->
2014-01-28 03:30:06 +00:00
fs.readFile filename, < span class = "function" > < span class = "params" > (err, contents)< / span > -> < / span >
2016-09-22 20:19:43 +00:00
compile filename, contents.toString()
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > filename, fn, i, len;
2011-09-21 03:42:19 +00:00
2016-09-22 20:19:43 +00:00
fn = < span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > filename< / span > ) < / span > {
< span class = "keyword" > return< / span > fs.readFile(filename, < span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > err, contents< / span > ) < / span > {
2014-01-28 03:30:06 +00:00
< span class = "keyword" > return< / span > compile(filename, contents.toString());
2010-12-23 06:43:13 +00:00
});
};
2015-02-18 20:43:33 +00:00
< span class = "keyword" > for< / span > (i = < span class = "number" > 0< / span > , len = list.length; i < len; i++) {
filename = list[i];
fn(filename);
2010-12-23 06:43:13 +00:00
}
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example14 = "for filename in list\n do (filename) ->\n fs.readFile filename, (err, contents) ->\n compile filename, contents.toString()\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example14);' > load< / div > < br class = 'clear' / > < / div >
2010-12-23 06:43:13 +00:00
2010-12-04 17:52:51 +00:00
< p >
< span id = "slices" class = "bookmark" > < / span >
< b class = "header" > Array Slicing and Splicing with Ranges< / b >
Ranges can also be used to extract slices of arrays.
2015-09-03 16:37:36 +00:00
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 > ).
2012-04-10 18:57:45 +00:00
Slices indices have useful defaults. An omitted first index defaults to
zero and an omitted second index defaults to the size of the array.
2010-12-04 17:52:51 +00:00
< / p >
2014-01-28 03:30:06 +00:00
< 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 > ]
2010-12-04 17:52:51 +00:00
2014-01-28 03:30:06 +00:00
start = numbers[< span class = "number" > 0.< / span > < span class = "number" > .2< / span > ]
2010-12-04 17:52:51 +00:00
2016-09-22 20:19:43 +00:00
middle = numbers[< span class = "number" > 3.< / span > ..< span class = "number" > -2< / span > ]
2010-12-04 17:52:51 +00:00
2016-09-22 20:19:43 +00:00
end = numbers[< span class = "number" > -2.< / span > .]
2010-12-13 02:41:04 +00:00
2014-01-28 01:56:29 +00:00
copy = numbers[..]
2014-01-28 03:30:06 +00:00
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > copy, end, middle, numbers, start;
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
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 > ];
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
start = numbers.slice(< span class = "number" > 0< / span > , < span class = "number" > 3< / span > );
2011-09-21 03:42:19 +00:00
2016-09-22 20:19:43 +00:00
middle = numbers.slice(< span class = "number" > 3< / span > , < span class = "number" > -2< / span > );
2011-09-21 03:42:19 +00:00
2016-09-22 20:19:43 +00:00
end = numbers.slice(< span class = "number" > -2< / span > );
2012-04-10 18:57:45 +00:00
2014-01-28 03:30:06 +00:00
copy = numbers.slice(< span class = "number" > 0< / span > );
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example15 = "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 > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example15);' > load< / div > < div class = "minibutton ok" onclick = "javascript: var copy , end , middle , numbers , start ;
2012-04-10 18:57:45 +00:00
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
start = numbers.slice(0, 3);
2011-09-21 03:42:19 +00:00
2014-01-28 01:56:29 +00:00
middle = numbers.slice(3, -2);
2011-09-21 03:42:19 +00:00
2014-01-28 01:56:29 +00:00
end = numbers.slice(-2);
2012-04-10 18:57:45 +00:00
copy = numbers.slice(0);
2016-12-01 06:28:00 +00:00
;alert(middle);">run: middle< / div > < br class = 'clear' / > < / div >
2010-12-04 17:52:51 +00:00
< p >
The same syntax can be used with assignment to replace a segment of an array
with new values, splicing it.
< / p >
2014-01-28 03:30:06 +00:00
< 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 > ]
2010-12-04 17:52:51 +00:00
2016-09-22 20:19:43 +00:00
numbers[< 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 > ]
2015-09-03 16:37:36 +00:00
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > numbers, ref;
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
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 > ];
2011-09-21 03:42:19 +00:00
2016-09-22 20:19:43 +00:00
[].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;
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example16 = "numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n\nnumbers[3..6] = [-3, -4, -5, -6]\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example16);' > load< / div > < div class = "minibutton ok" onclick = "javascript: var numbers , ref ;
2011-09-21 03:42:19 +00:00
2010-12-04 17:52:51 +00:00
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
2011-09-21 03:42:19 +00:00
2015-02-18 20:43:33 +00:00
[].splice.apply(numbers, [3, 4].concat(ref = [-3, -4, -5, -6])), ref;
2016-12-01 06:28:00 +00:00
;alert(numbers);">run: numbers< / div > < br class = 'clear' / > < / div >
2010-12-04 17:52:51 +00:00
< p >
2016-12-01 06:28:00 +00:00
Note that JavaScript strings are immutable, and can’ t be spliced.
2010-12-04 17:52:51 +00:00
< / p >
2010-02-21 19:30:21 +00:00
< p >
< span id = "expressions" class = "bookmark" > < / span >
2010-01-05 03:51:02 +00:00
< b class = "header" > Everything is an Expression (at least, as much as possible)< / b >
2016-12-01 06:28:00 +00:00
You might have noticed how even though we don’ t add return statements
2010-01-05 03:51:02 +00:00
to CoffeeScript functions, they nonetheless return their final value.
The CoffeeScript compiler tries to make sure that all statements in the
2015-09-03 16:37:36 +00:00
language can be used as expressions. Watch how the < code > return< / code > gets
2011-07-09 05:26:30 +00:00
pushed down into each possible branch of execution in the function
2010-01-05 03:51:02 +00:00
below.
< / p >
2014-01-28 03:30:06 +00:00
< div class = 'code' > < pre > < code > < span class = "function" > < span class = "title" > grade< / span > = < span class = "params" > (student)< / span > -> < / span >
< span class = "keyword" > if< / span > student.excellentWork
< span class = "string" > "A+"< / span >
< span class = "keyword" > else< / span > < span class = "keyword" > if< / span > student.okayStuff
< 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 >
< span class = "keyword" > else< / span >
< span class = "string" > "C"< / span >
2016-09-22 20:19:43 +00:00
eldest = < 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 >
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > eldest, grade;
2014-01-28 03:30:06 +00:00
2016-09-22 20:19:43 +00:00
grade = < span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > student< / span > ) < / span > {
2014-01-28 03:30:06 +00:00
< span class = "keyword" > if< / span > (student.excellentWork) {
< span class = "keyword" > return< / span > < span class = "string" > "A+"< / span > ;
} < span class = "keyword" > else< / span > < span class = "keyword" > if< / span > (student.okayStuff) {
< span class = "keyword" > if< / span > (student.triedHard) {
< span class = "keyword" > return< / span > < span class = "string" > "B"< / span > ;
} < span class = "keyword" > else< / span > {
< span class = "keyword" > return< / span > < span class = "string" > "B-"< / span > ;
2010-11-21 17:38:27 +00:00
}
2014-01-28 03:30:06 +00:00
} < span class = "keyword" > else< / span > {
< span class = "keyword" > return< / span > < span class = "string" > "C"< / span > ;
2010-11-21 17:38:27 +00:00
}
2010-01-05 03:51:02 +00:00
};
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
eldest = < span class = "number" > 24< / span > > < span class = "number" > 21< / span > ? < span class = "string" > "Liz"< / span > : < span class = "string" > "Ike"< / span > ;
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example17 = "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 > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example17);' > load< / div > < div class = "minibutton ok" onclick = "javascript: var eldest , grade ;
2011-09-21 03:42:19 +00:00
2010-05-15 05:18:05 +00:00
grade = function(student) {
2010-11-21 17:38:27 +00:00
if (student.excellentWork) {
2016-12-01 06:28:00 +00:00
return " A+" ;
2010-11-21 17:38:27 +00:00
} else if (student.okayStuff) {
if (student.triedHard) {
2016-12-01 06:28:00 +00:00
return " B" ;
2010-11-21 17:38:27 +00:00
} else {
2016-12-01 06:28:00 +00:00
return " B-" ;
2010-11-21 17:38:27 +00:00
}
} else {
2016-12-01 06:28:00 +00:00
return " C" ;
2010-11-21 17:38:27 +00:00
}
2010-01-05 03:51:02 +00:00
};
2011-09-21 03:42:19 +00:00
2016-12-01 06:28:00 +00:00
eldest = 24 > 21 ? " Liz" : " Ike" ;
;alert(eldest);">run: eldest< / div > < br class = 'clear' / > < / div >
2010-01-10 22:14:20 +00:00
< p >
2016-12-01 06:28:00 +00:00
Even though functions will always return their final value, it’ s both possible
2010-01-10 22:14:20 +00:00
and encouraged to return early from a function body writing out the explicit
2016-12-01 06:28:00 +00:00
return (< code > return value< / code > ), when you know that you’ re done.
2010-01-10 22:14:20 +00:00
< / p >
2010-01-05 03:51:02 +00:00
< p >
Because variable declarations occur at the top of scope, assignment can
2016-12-01 06:28:00 +00:00
be used within expressions, even for variables that haven’ t been seen before:
2010-01-05 03:51:02 +00:00
< / p >
2014-01-28 03:30:06 +00:00
< div class = 'code' > < pre > < code > six = (one = < span class = "number" > 1< / span > ) + (two = < span class = "number" > 2< / span > ) + (three = < span class = "number" > 3< / span > )
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > one, six, three, two;
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
six = (one = < span class = "number" > 1< / span > ) + (two = < span class = "number" > 2< / span > ) + (three = < span class = "number" > 3< / span > );
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example18 = "six = (one = 1) + (two = 2) + (three = 3)\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example18);' > load< / div > < div class = "minibutton ok" onclick = "javascript: var one , six , three , two ;
2011-09-21 03:42:19 +00:00
six = (one = 1) + (two = 2) + (three = 3);
2016-12-01 06:28:00 +00:00
;alert(six);">run: six< / div > < br class = 'clear' / > < / div >
2010-01-05 03:51:02 +00:00
< 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 >
2014-01-28 03:30:06 +00:00
< div class = 'code' > < pre > < code > < span class = "comment" > # The first ten global properties.< / span >
2010-01-05 03:51:02 +00:00
2016-09-22 20:19:43 +00:00
globals = (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 > ]
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > globals, name;
2011-09-21 03:42:19 +00:00
2016-09-22 20:19:43 +00:00
globals = ((< span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > < / span > ) < / span > {
2015-02-18 20:43:33 +00:00
< span class = "keyword" > var< / span > results;
results = [];
2016-09-22 20:19:43 +00:00
< span class = "keyword" > for< / span > (name < span class = "keyword" > in< / span > < span class = "built_in" > window< / span > ) {
2015-02-18 20:43:33 +00:00
results.push(name);
2010-07-17 14:28:06 +00:00
}
2015-02-18 20:43:33 +00:00
< span class = "keyword" > return< / span > results;
2014-01-28 03:30:06 +00:00
})()).slice(< span class = "number" > 0< / span > , < span class = "number" > 10< / span > );
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example19 = "# The first ten global properties.\n\nglobals = (name for name of window)[0...10]\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example19);' > load< / div > < div class = "minibutton ok" onclick = "javascript: var globals , name ;
2011-09-21 03:42:19 +00:00
2010-12-23 20:41:42 +00:00
globals = ((function() {
2015-02-18 20:43:33 +00:00
var results;
results = [];
2010-11-22 00:53:31 +00:00
for (name in window) {
2015-02-18 20:43:33 +00:00
results.push(name);
2010-07-17 14:28:06 +00:00
}
2015-02-18 20:43:33 +00:00
return results;
2011-09-21 03:42:19 +00:00
})()).slice(0, 10);
2016-12-01 06:28:00 +00:00
;alert(globals);">run: globals< / div > < br class = 'clear' / > < / div >
2010-01-05 03:51:02 +00:00
< p >
As well as silly things, like passing a < b > try/catch< / b > statement directly
into a function call:
< / p >
2014-01-28 01:56:29 +00:00
< div class = 'code' > < pre > < code > alert(
2014-01-28 03:30:06 +00:00
< span class = "keyword" > try< / span >
nonexistent / < span class = "literal" > undefined< / span >
< span class = "keyword" > catch< / span > error
< span class = "string" > "And the error is ... < span class = "subst" > #{error}< / span > "< / span >
2010-01-05 03:51:02 +00:00
)
2014-01-28 03:30:06 +00:00
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > error;
2013-03-18 05:06:33 +00:00
2016-09-22 20:19:43 +00:00
alert((< span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > < / span > ) < / span > {
2014-01-28 03:30:06 +00:00
< span class = "keyword" > try< / span > {
< span class = "keyword" > return< / span > nonexistent / < span class = "keyword" > void< / span > < span class = "number" > 0< / span > ;
2015-09-03 16:37:36 +00:00
} < span class = "keyword" > catch< / span > (error1) {
error = error1;
2014-01-28 03:30:06 +00:00
< span class = "keyword" > return< / span > < span class = "string" > "And the error is ... "< / span > + error;
2010-01-05 03:51:02 +00:00
}
2010-12-23 20:41:42 +00:00
})());
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example20 = "alert(\n try\n nonexistent / undefined\n catch error\n \"And the error is ... #{error}\"\n)\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example20);' > load< / div > < div class = "minibutton ok" onclick = "javascript: var error ;
2013-03-18 05:06:33 +00:00
2011-09-21 03:42:19 +00:00
alert((function() {
2010-01-05 03:51:02 +00:00
try {
2010-11-21 17:38:27 +00:00
return nonexistent / void 0;
2015-09-03 16:37:36 +00:00
} catch (error1) {
error = error1;
2016-12-01 06:28:00 +00:00
return " And the error is ... " + error;
2010-01-05 03:51:02 +00:00
}
2011-09-21 03:42:19 +00:00
})());
2016-12-01 06:28:00 +00:00
;">run< / div > < br class = 'clear' / > < / div >
2010-01-17 23:12:59 +00:00
< p >
2016-12-01 06:28:00 +00:00
There are a handful of statements in JavaScript that can’ t be meaningfully
2015-09-03 16:37:36 +00:00
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,
2016-12-01 06:28:00 +00:00
CoffeeScript won’ t try to perform the conversion.
2010-01-17 23:12:59 +00:00
< / p >
2010-12-23 06:43:13 +00:00
2010-12-22 03:24:24 +00:00
< p >
< span id = "operators" class = "bookmark" > < / span >
< b class = "header" > Operators and Aliases< / b >
2015-09-03 16:37:36 +00:00
Because the < code > ==< / code > operator frequently causes undesirable coercion,
2010-12-22 03:24:24 +00:00
is intransitive, and has a different meaning than in other languages,
2015-09-03 16:37:36 +00:00
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 > .
2010-12-22 03:24:24 +00:00
< / p >
< p >
2015-09-03 16:37:36 +00:00
You can use < code > not< / code > as an alias for < code > !< / code > .
2010-12-22 03:24:24 +00:00
< / p >
< p >
2015-09-03 16:37:36 +00:00
For logic, < code > and< / code > compiles to < code > & & < / code > , and < code > or< / code >
into < code > ||< / code > .
2010-12-22 03:24:24 +00:00
< / p >
< p >
2015-09-03 16:37:36 +00:00
Instead of a newline or semicolon, < code > then< / code > can be used to separate
2010-12-22 03:24:24 +00:00
conditions from expressions, in < b > while< / b > ,
< b > if< / b > /< b > else< / b > , and < b > switch< / b > /< b > when< / b > statements.
< / p >
< p >
2015-09-03 16:37:36 +00:00
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 > .
2010-12-22 03:24:24 +00:00
< / p >
< p >
2015-09-03 16:37:36 +00:00
< code > unless< / code > can be used as the inverse of < code > if< / code > .
2010-12-22 03:24:24 +00:00
< / p >
< p >
2015-09-03 16:37:36 +00:00
As a shortcut for < code > this.property< / code > , you can use < code > @property< / code > .
2010-12-22 03:24:24 +00:00
< / p >
< p >
2015-09-03 16:37:36 +00:00
You can use < code > in< / code > to test for array presence, and < code > of< / code > to
2010-12-22 03:24:24 +00:00
test for JavaScript object-key presence.
< / p >
2014-01-28 01:56:29 +00:00
< p >
2015-09-03 16:37:36 +00:00
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
2015-05-14 09:23:10 +00:00
< a href = "http://en.wikipedia.org/wiki/Modulo_operation" > “dividend dependent modulo”< / a > :
2014-01-28 01:56:29 +00:00
< / p >
2016-09-22 20:19:43 +00:00
< 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 >
< 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 >
2015-05-27 15:57:31 +00:00
tabs.selectTabAtIndex((tabs.currentIndex - count) %% tabs.length)
2016-09-22 20:19:43 +00:00
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > modulo = < span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > a, b< / span > ) < / span > { < span class = "keyword" > return< / span > (+a % (b = +b) + b) % b; };
2015-05-27 15:57:31 +00:00
2016-09-22 20:19:43 +00:00
< span class = "number" > -7< / span > % < span class = "number" > 5< / span > === < span class = "number" > -2< / span > ;
2015-05-27 15:57:31 +00:00
2016-09-22 20:19:43 +00:00
modulo(< span class = "number" > -7< / span > , < span class = "number" > 5< / span > ) === < span class = "number" > 3< / span > ;
2015-05-27 15:57:31 +00:00
tabs.selectTabAtIndex(modulo(tabs.currentIndex - count, tabs.length));
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example21 = "-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 > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example21);' > load< / div > < br class = 'clear' / > < / div >
2010-12-22 03:24:24 +00:00
< p >
All together now:
< / p >
< table class = "definitions" >
< tr > < th > CoffeeScript< / th > < th > JavaScript< / th > < / tr >
2015-09-03 16:37:36 +00:00
< tr > < td > < code > is< / code > < / td > < td > < code > ===< / code > < / td > < / tr >
< tr > < td > < code > isnt< / code > < / td > < td > < code > !==< / code > < / td > < / tr >
< tr > < td > < code > not< / code > < / td > < td > < code > !< / code > < / td > < / tr >
< tr > < td > < code > and< / code > < / td > < td > < code > & & < / code > < / td > < / tr >
< tr > < td > < code > or< / code > < / td > < td > < code > ||< / code > < / td > < / tr >
< tr > < td > < code > true< / code > , < code > yes< / code > , < code > on< / code > < / td > < td > < code > true< / code > < / td > < / tr >
< tr > < td > < code > false< / code > , < code > no< / code > , < code > off< / code > < / td > < td > < code > false< / code > < / td > < / tr >
< tr > < td > < code > @< / code > , < code > this< / code > < / td > < td > < code > this< / code > < / td > < / tr >
< tr > < td > < code > of< / code > < / td > < td > < code > in< / code > < / td > < / tr >
< tr > < td > < code > in< / code > < / td > < td > < i > < small > no JS equivalent< / small > < / i > < / td > < / tr >
< tr > < td > < code > a ** b< / code > < / td > < td > < code > Math.pow(a, b)< / code > < / td > < / tr >
< tr > < td > < code > a // b< / code > < / td > < td > < code > Math.floor(a / b)< / code > < / td > < / tr >
< tr > < td > < code > a %% b< / code > < / td > < td > < code > (a % b + b) % b< / code > < / td > < / tr >
2010-12-22 03:24:24 +00:00
< / table >
2014-01-28 03:30:06 +00:00
< div class = 'code' > < pre > < code > launch() < span class = "keyword" > if< / span > ignition < span class = "keyword" > is< / span > < span class = "literal" > on< / span >
2010-12-22 03:24:24 +00:00
2014-01-28 03:30:06 +00:00
volume = < span class = "number" > 10< / span > < span class = "keyword" > if< / span > band < span class = "keyword" > isnt< / span > SpinalTap
2010-12-22 03:24:24 +00:00
2014-01-28 03:30:06 +00:00
letTheWildRumpusBegin() < span class = "keyword" > unless< / span > answer < span class = "keyword" > is< / span > < span class = "literal" > no< / span >
2010-12-22 03:24:24 +00:00
2014-01-28 03:30:06 +00:00
< span class = "keyword" > if< / span > car.speed < limit < span class = "keyword" > then< / span > accelerate()
2010-12-22 03:24:24 +00:00
2014-01-28 03:30:06 +00:00
winner = < 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 > ]
2010-12-22 03:24:24 +00:00
2016-09-22 20:19:43 +00:00
< span class = "built_in" > print< / span > inspect < span class = "string" > "My name is < span class = "subst" > #{@name}< / span > "< / span >
2014-01-28 03:30:06 +00:00
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > volume, winner;
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
< span class = "keyword" > if< / span > (ignition === < span class = "literal" > true< / span > ) {
2012-04-10 18:57:45 +00:00
launch();
}
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
< span class = "keyword" > if< / span > (band !== SpinalTap) {
volume = < span class = "number" > 10< / span > ;
2012-04-10 18:57:45 +00:00
}
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
< span class = "keyword" > if< / span > (answer !== < span class = "literal" > false< / span > ) {
2012-04-10 18:57:45 +00:00
letTheWildRumpusBegin();
}
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
< span class = "keyword" > if< / span > (car.speed < limit) {
2012-04-10 18:57:45 +00:00
accelerate();
}
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
< span class = "keyword" > if< / span > (pick === < span class = "number" > 47< / span > || pick === < span class = "number" > 92< / span > || pick === < span class = "number" > 13< / span > ) {
winner = < span class = "literal" > true< / span > ;
2012-04-10 18:57:45 +00:00
}
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
print(inspect(< span class = "string" > "My name is "< / span > + < span class = "keyword" > this< / span > .name));
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example22 = "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 > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example22);' > load< / div > < br class = 'clear' / > < / div >
2010-02-13 20:25:04 +00:00
2010-02-21 19:30:21 +00:00
< p >
2016-11-21 01:05:19 +00:00
< span id = "existential-operator" class = "bookmark" > < / span >
2010-01-27 04:23:59 +00:00
< b class = "header" > The Existential Operator< / b >
2016-12-01 06:28:00 +00:00
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
2010-01-27 04:23:59 +00:00
a variable is < b > null< / b > or < b > undefined< / b > , which makes it analogous
2016-12-01 06:28:00 +00:00
to Ruby’ s < code > nil?< / code >
2010-01-27 04:23:59 +00:00
< / p >
< p >
2015-09-03 16:37:36 +00:00
It can also be used for safer conditional assignment than < code > ||=< / code >
2010-01-27 04:23:59 +00:00
provides, for cases where you may be handling numbers or strings.
< / p >
2014-01-28 03:30:06 +00:00
< 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?
2010-01-27 04:23:59 +00:00
2014-01-28 03:30:06 +00:00
speed = < span class = "number" > 0< / span >
speed ?= < span class = "number" > 15< / span >
2010-01-27 04:23:59 +00:00
2014-01-28 03:30:06 +00:00
footprints = yeti ? < span class = "string" > "bear"< / span >
2015-09-03 16:37:36 +00:00
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > footprints, solipsism, speed;
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
< 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 > )) {
solipsism = < span class = "literal" > true< / span > ;
2010-01-27 04:23:59 +00:00
}
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
speed = < span class = "number" > 0< / span > ;
2012-04-10 18:57:45 +00:00
2014-01-28 03:30:06 +00:00
< span class = "keyword" > if< / span > (speed == < span class = "literal" > null< / span > ) {
speed = < span class = "number" > 15< / span > ;
2012-04-10 18:57:45 +00:00
}
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
footprints = < span class = "keyword" > typeof< / span > yeti !== < span class = "string" > "undefined"< / span > & & yeti !== < span class = "literal" > null< / span > ? yeti : < span class = "string" > "bear"< / span > ;
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example23 = "solipsism = true if mind? and not world?\n\nspeed = 0\nspeed ?= 15\n\nfootprints = yeti ? \"bear\"\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example23);' > load< / div > < div class = "minibutton ok" onclick = "javascript: var footprints , solipsism , speed ;
2011-09-21 03:42:19 +00:00
2016-12-01 06:28:00 +00:00
if ((typeof mind !== " undefined" & & mind !== null) & & (typeof world === " undefined" || world === null)) {
2010-01-27 04:23:59 +00:00
solipsism = true;
}
2011-09-21 03:42:19 +00:00
2012-04-10 18:57:45 +00:00
speed = 0;
if (speed == null) {
speed = 15;
}
2011-09-21 03:42:19 +00:00
2016-12-01 06:28:00 +00:00
footprints = typeof yeti !== " undefined" & & yeti !== null ? yeti : " bear" ;
;alert(footprints);">run: footprints< / div > < br class = 'clear' / > < / div >
2010-01-27 04:23:59 +00:00
< p >
2015-09-03 16:37:36 +00:00
The accessor variant of the existential operator < code > ?.< / code > can be used to soak
2010-01-27 04:23:59 +00:00
up null references in a chain of properties. Use it instead
2015-09-03 16:37:36 +00:00
of the dot accessor < code > .< / code > in cases where the base value may be < b > null< / b >
2016-12-01 06:28:00 +00:00
or < b > undefined< / b > . If all of the properties exist then you’ ll get the expected
2010-01-27 04:23:59 +00:00
result, if the chain is broken, < b > undefined< / b > is returned instead of
the < b > TypeError< / b > that would be raised otherwise.
< / p >
2014-01-28 01:56:29 +00:00
< div class = 'code' > < pre > < code > zip = lottery.drawWinner?().address?.zipcode
2015-02-18 20:43:33 +00:00
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > ref, zip;
2011-09-21 03:42:19 +00:00
2015-02-18 20:43:33 +00:00
zip = < 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 > ;
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example24 = "zip = lottery.drawWinner?().address?.zipcode\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example24);' > load< / div > < br class = 'clear' / > < / div >
2010-01-27 04:23:59 +00:00
< p >
2016-12-01 06:28:00 +00:00
Soaking up nulls is similar to Ruby’ s
2015-01-29 17:20:46 +00:00
< a href = "https://rubygems.org/gems/andand" > andand gem< / a > , and to the
2016-09-22 20:19:43 +00:00
< a href = "http://docs.groovy-lang.org/latest/html/documentation/index.html#_safe_navigation_operator" > safe navigation operator< / a >
2010-01-27 04:23:59 +00:00
in Groovy.
< / p >
2009-12-21 16:41:45 +00:00
2010-02-21 19:30:21 +00:00
< p >
2010-02-28 01:12:47 +00:00
< span id = "classes" class = "bookmark" > < / span >
< b class = "header" > Classes, Inheritance, and Super< / b >
2016-12-01 06:28:00 +00:00
JavaScript’ s prototypal inheritance has always been a bit of a
2009-12-24 00:42:18 +00:00
brain-bender, with a whole family tree of libraries that provide a cleaner
2016-12-01 06:28:00 +00:00
syntax for classical inheritance on top of JavaScript’ s prototypes:
2009-12-24 01:24:55 +00:00
< a href = "http://code.google.com/p/base2/" > Base2< / a > ,
2009-12-24 00:42:18 +00:00
< a href = "http://prototypejs.org/" > Prototype.js< / a > ,
< a href = "http://jsclass.jcoglan.com/" > JS.Class< / a > , etc.
2009-12-24 01:48:55 +00:00
The libraries provide syntactic sugar, but the built-in inheritance would
2016-12-01 06:28:00 +00:00
be completely usable if it weren’ t for a couple of small exceptions:
it’ s awkward to call < b > super< / b > (the prototype object’ s
implementation of the current function), and it’ s awkward to correctly
2010-01-12 03:55:01 +00:00
set the prototype chain.
2010-01-10 22:14:20 +00:00
< / p >
< p >
2010-02-28 01:12:47 +00:00
Instead of repetitively attaching functions to a prototype, CoffeeScript
2015-09-03 16:37:36 +00:00
provides a basic < code > class< / code > structure that allows you to name your class,
2010-02-28 01:12:47 +00:00
set the superclass, assign prototypal properties, and define the constructor,
in a single assignable expression.
2009-12-24 00:42:18 +00:00
< / p >
2010-11-22 02:12:59 +00:00
< p >
2011-05-01 16:08:15 +00:00
Constructor functions are named, to better support helpful stack traces.
2015-09-03 16:37:36 +00:00
In the first class in the example below, < code > this.constructor.name is "Animal"< / code > .
2010-11-22 02:12:59 +00:00
< / p >
2014-01-28 03:30:06 +00:00
< div class = 'code' > < pre > < code > < span class = "class" > < span class = "keyword" > class< / span > < span class = "title" > Animal< / span > < / span >
2016-09-22 20:19:43 +00:00
constructor: < span class = "function" > < span class = "params" > (@name)< / span > -> < / span >
2010-07-29 04:51:35 +00:00
2016-09-22 20:19:43 +00:00
move: < span class = "function" > < span class = "params" > (meters)< / span > -> < / span >
alert @name + < span class = "string" > " moved < span class = "subst" > #{meters}< / span > m."< / span >
2010-02-13 20:25:04 +00:00
2014-01-28 03:30:06 +00:00
< span class = "class" > < span class = "keyword" > class< / span > < span class = "title" > Snake< / span > < span class = "keyword" > extends< / span > < span class = "title" > Animal< / span > < / span >
2016-09-22 20:19:43 +00:00
move: < span class = "function" > -> < / span >
2014-01-28 03:30:06 +00:00
alert < span class = "string" > "Slithering..."< / span >
< span class = "keyword" > super< / span > < span class = "number" > 5< / span >
2010-02-13 20:25:04 +00:00
2014-01-28 03:30:06 +00:00
< span class = "class" > < span class = "keyword" > class< / span > < span class = "title" > Horse< / span > < span class = "keyword" > extends< / span > < span class = "title" > Animal< / span > < / span >
2016-09-22 20:19:43 +00:00
move: < span class = "function" > -> < / span >
2014-01-28 03:30:06 +00:00
alert < span class = "string" > "Galloping..."< / span >
< span class = "keyword" > super< / span > < span class = "number" > 45< / span >
2009-12-21 16:41:45 +00:00
2014-01-28 03:30:06 +00:00
sam = < span class = "keyword" > new< / span > Snake < span class = "string" > "Sammy the Python"< / span >
tom = < span class = "keyword" > new< / span > Horse < span class = "string" > "Tommy the Palomino"< / span >
2009-12-21 16:41:45 +00:00
sam.move()
tom.move()
2014-01-28 03:30:06 +00:00
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > Animal, Horse, Snake, sam, tom,
2016-09-22 20:19:43 +00:00
extend = < span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > child, parent< / span > ) < / 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; },
2015-02-18 20:43:33 +00:00
hasProp = {}.hasOwnProperty;
2011-09-21 03:42:19 +00:00
2016-09-22 20:19:43 +00:00
Animal = (< span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > < / span > ) < / span > {
< span class = "function" > < span class = "keyword" > function< / span > < span class = "title" > Animal< / span > (< span class = "params" > name< / span > ) < / span > {
2015-02-18 20:43:33 +00:00
< span class = "keyword" > this< / span > .name = name;
2010-10-25 01:50:34 +00:00
}
2011-09-21 03:42:19 +00:00
2016-09-22 20:19:43 +00:00
Animal.prototype.move = < span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > meters< / span > ) < / span > {
2014-01-28 03:30:06 +00:00
< span class = "keyword" > return< / span > alert(< span class = "keyword" > this< / span > .name + (< span class = "string" > " moved "< / span > + meters + < span class = "string" > "m."< / span > ));
2010-11-21 17:38:27 +00:00
};
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
< span class = "keyword" > return< / span > Animal;
2011-09-21 03:42:19 +00:00
2010-12-23 20:41:42 +00:00
})();
2011-09-21 03:42:19 +00:00
2016-09-22 20:19:43 +00:00
Snake = (< span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > superClass< / span > ) < / span > {
2015-02-18 20:43:33 +00:00
extend(Snake, superClass);
2011-09-21 03:42:19 +00:00
2016-09-22 20:19:43 +00:00
< span class = "function" > < span class = "keyword" > function< / span > < span class = "title" > Snake< / span > (< span class = "params" > < / span > ) < / span > {
2014-01-28 03:30:06 +00:00
< span class = "keyword" > return< / span > Snake.__super__.constructor.apply(< span class = "keyword" > this< / span > , < span class = "built_in" > arguments< / span > );
2010-10-25 01:50:34 +00:00
}
2011-09-21 03:42:19 +00:00
2016-09-22 20:19:43 +00:00
Snake.prototype.move = < span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > < / span > ) < / span > {
2014-01-28 03:30:06 +00:00
alert(< span class = "string" > "Slithering..."< / span > );
< span class = "keyword" > return< / span > Snake.__super__.move.call(< span class = "keyword" > this< / span > , < span class = "number" > 5< / span > );
2010-11-21 17:38:27 +00:00
};
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
< span class = "keyword" > return< / span > Snake;
2011-09-21 03:42:19 +00:00
2011-12-18 18:41:33 +00:00
})(Animal);
2011-09-21 03:42:19 +00:00
2016-09-22 20:19:43 +00:00
Horse = (< span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > superClass< / span > ) < / span > {
2015-02-18 20:43:33 +00:00
extend(Horse, superClass);
2011-09-21 03:42:19 +00:00
2016-09-22 20:19:43 +00:00
< span class = "function" > < span class = "keyword" > function< / span > < span class = "title" > Horse< / span > (< span class = "params" > < / span > ) < / span > {
2014-01-28 03:30:06 +00:00
< span class = "keyword" > return< / span > Horse.__super__.constructor.apply(< span class = "keyword" > this< / span > , < span class = "built_in" > arguments< / span > );
2010-10-25 01:50:34 +00:00
}
2011-09-21 03:42:19 +00:00
2016-09-22 20:19:43 +00:00
Horse.prototype.move = < span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > < / span > ) < / span > {
2014-01-28 03:30:06 +00:00
alert(< span class = "string" > "Galloping..."< / span > );
< span class = "keyword" > return< / span > Horse.__super__.move.call(< span class = "keyword" > this< / span > , < span class = "number" > 45< / span > );
2010-11-21 17:38:27 +00:00
};
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
< span class = "keyword" > return< / span > Horse;
2011-09-21 03:42:19 +00:00
2011-12-18 18:41:33 +00:00
})(Animal);
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
sam = < span class = "keyword" > new< / span > Snake(< span class = "string" > "Sammy the Python"< / span > );
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
tom = < span class = "keyword" > new< / span > Horse(< span class = "string" > "Tommy the Palomino"< / span > );
2011-09-21 03:42:19 +00:00
2009-12-21 16:41:45 +00:00
sam.move();
2011-09-21 03:42:19 +00:00
2009-12-21 16:41:45 +00:00
tom.move();
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example25 = "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 > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example25);' > load< / div > < div class = "minibutton ok" onclick = "javascript: var Animal , Horse , Snake , sam , tom ,
2015-02-18 20:43:33 +00:00
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; },
hasProp = {}.hasOwnProperty;
2011-09-21 03:42:19 +00:00
2010-12-23 20:41:42 +00:00
Animal = (function() {
2015-02-18 20:43:33 +00:00
function Animal(name) {
this.name = name;
2010-10-25 01:50:34 +00:00
}
2011-09-21 03:42:19 +00:00
2010-11-21 17:38:27 +00:00
Animal.prototype.move = function(meters) {
2016-12-01 06:28:00 +00:00
return alert(this.name + (" moved " + meters + " m." ));
2010-11-21 17:38:27 +00:00
};
2011-09-21 03:42:19 +00:00
2010-10-20 01:59:13 +00:00
return Animal;
2011-09-21 03:42:19 +00:00
2010-12-23 20:41:42 +00:00
})();
2011-09-21 03:42:19 +00:00
2015-02-18 20:43:33 +00:00
Snake = (function(superClass) {
extend(Snake, superClass);
2011-09-21 03:42:19 +00:00
2010-10-20 01:59:13 +00:00
function Snake() {
2014-01-28 01:56:29 +00:00
return Snake.__super__.constructor.apply(this, arguments);
2010-10-25 01:50:34 +00:00
}
2011-09-21 03:42:19 +00:00
2010-11-21 17:38:27 +00:00
Snake.prototype.move = function() {
2016-12-01 06:28:00 +00:00
alert(" Slithering..." );
2010-11-21 17:38:27 +00:00
return Snake.__super__.move.call(this, 5);
};
2011-09-21 03:42:19 +00:00
2010-10-20 01:59:13 +00:00
return Snake;
2011-09-21 03:42:19 +00:00
2011-12-18 18:41:33 +00:00
})(Animal);
2011-09-21 03:42:19 +00:00
2015-02-18 20:43:33 +00:00
Horse = (function(superClass) {
extend(Horse, superClass);
2011-09-21 03:42:19 +00:00
2010-10-20 01:59:13 +00:00
function Horse() {
2014-01-28 01:56:29 +00:00
return Horse.__super__.constructor.apply(this, arguments);
2010-10-25 01:50:34 +00:00
}
2011-09-21 03:42:19 +00:00
2010-11-21 17:38:27 +00:00
Horse.prototype.move = function() {
2016-12-01 06:28:00 +00:00
alert(" Galloping..." );
2010-11-21 17:38:27 +00:00
return Horse.__super__.move.call(this, 45);
};
2011-09-21 03:42:19 +00:00
2010-10-20 01:59:13 +00:00
return Horse;
2011-09-21 03:42:19 +00:00
2011-12-18 18:41:33 +00:00
})(Animal);
2011-09-21 03:42:19 +00:00
2016-12-01 06:28:00 +00:00
sam = new Snake(" Sammy the Python" );
2011-09-21 03:42:19 +00:00
2016-12-01 06:28:00 +00:00
tom = new Horse(" Tommy the Palomino" );
2011-09-21 03:42:19 +00:00
2009-12-21 16:41:45 +00:00
sam.move();
2011-09-21 03:42:19 +00:00
tom.move();
2016-12-01 06:28:00 +00:00
;">run< / div > < br class = 'clear' / > < / div >
2010-02-27 01:09:49 +00:00
< p >
2016-12-01 06:28:00 +00:00
If structuring your prototypes classically isn’ t your cup of tea, CoffeeScript
2015-09-03 16:37:36 +00:00
provides a couple of lower-level conveniences. The < code > extends< / code > operator
2011-05-01 16:08:15 +00:00
helps with proper prototype setup, and can be used to create an inheritance
2015-09-03 16:37:36 +00:00
chain between any pair of constructor functions; < code > ::< / code > gives you
2016-12-01 06:28:00 +00:00
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.
2010-02-27 01:09:49 +00:00
< / p >
2016-09-22 20:19:43 +00:00
< div class = 'code' > < pre > < code > String::dasherize = < span class = "function" > -> < / span >
2014-01-28 03:30:06 +00:00
< span class = "keyword" > this< / span > .replace < span class = "regexp" > /_/g< / span > , < span class = "string" > "-"< / span >
2016-09-22 20:19:43 +00:00
< / code > < / pre > < pre > < code > < span class = "built_in" > String< / span > .prototype.dasherize = < span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > < / span > ) < / span > {
2014-01-28 03:30:06 +00:00
< span class = "keyword" > return< / span > < span class = "keyword" > this< / span > .replace(< span class = "regexp" > /_/g< / span > , < span class = "string" > "-"< / span > );
2010-02-28 01:12:47 +00:00
};
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example26 = "String::dasherize = ->\n this.replace /_/g, \"-\"\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example26);' > load< / div > < div class = "minibutton ok" onclick = "javascript: String . prototype . dasherize = function() {
return this.replace(/_/g, " -" );
2011-09-21 03:42:19 +00:00
};
2016-12-01 06:28:00 +00:00
;alert(" one_two" .dasherize());">run: "one_two".dasherize()< / div > < br class = 'clear' / > < / div >
2010-04-04 00:43:50 +00:00
< p >
2011-07-09 05:26:30 +00:00
Finally, class definitions are blocks of executable code, which make for interesting
2010-11-22 02:12:59 +00:00
metaprogramming possibilities. Because in the context of a class definition,
2015-09-03 16:37:36 +00:00
< code > this< / code > is the class object itself (the constructor function), you
can assign static properties by using < br / > < code > @property: value< / code > , and call
functions defined in parent classes: < code > @attr 'title', type: 'text'< / code >
2010-04-04 00:43:50 +00:00
< / p >
2009-12-21 16:41:45 +00:00
2010-02-21 19:30:21 +00:00
< p >
2010-12-23 21:30:35 +00:00
< span id = "destructuring" class = "bookmark" > < / span >
< b class = "header" > Destructuring Assignment< / b >
2015-09-03 16:37:36 +00:00
Just like JavaScript (since ES2015), CoffeeScript has destructuring assignment
2010-01-12 03:55:01 +00:00
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 >
2014-01-28 03:30:06 +00:00
< div class = 'code' > < pre > < code > theBait = < span class = "number" > 1000< / span >
theSwitch = < span class = "number" > 0< / span >
2010-06-28 04:19:58 +00:00
2014-01-28 01:56:29 +00:00
[theBait, theSwitch] = [theSwitch, theBait]
2015-09-03 16:37:36 +00:00
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > ref, theBait, theSwitch;
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
theBait = < span class = "number" > 1000< / span > ;
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
theSwitch = < span class = "number" > 0< / span > ;
2011-09-21 03:42:19 +00:00
2015-02-18 20:43:33 +00:00
ref = [theSwitch, theBait], theBait = ref[< span class = "number" > 0< / span > ], theSwitch = ref[< span class = "number" > 1< / span > ];
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example27 = "theBait = 1000\ntheSwitch = 0\n\n[theBait, theSwitch] = [theSwitch, theBait]\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example27);' > load< / div > < div class = "minibutton ok" onclick = "javascript: var ref , theBait , theSwitch ;
2011-09-21 03:42:19 +00:00
2010-06-28 04:19:58 +00:00
theBait = 1000;
2011-09-21 03:42:19 +00:00
2010-06-28 04:19:58 +00:00
theSwitch = 0;
2011-09-21 03:42:19 +00:00
2015-02-18 20:43:33 +00:00
ref = [theSwitch, theBait], theBait = ref[0], theSwitch = ref[1];
2016-12-01 06:28:00 +00:00
;alert(theBait);">run: theBait< / div > < br class = 'clear' / > < / div >
2010-01-12 03:55:01 +00:00
< p >
2016-12-01 06:28:00 +00:00
But it’ s also helpful for dealing with functions that return multiple
2010-01-12 03:55:01 +00:00
values.
< / p >
2014-01-28 03:30:06 +00:00
< div class = 'code' > < pre > < code > < span class = "function" > < span class = "title" > weatherReport< / span > = < span class = "params" > (location)< / span > -> < / span >
< span class = "comment" > # Make an Ajax request to fetch the weather...< / span >
[location, < span class = "number" > 72< / span > , < span class = "string" > "Mostly Sunny"< / span > ]
2010-01-12 03:55:01 +00:00
2014-01-28 03:30:06 +00:00
[city, temp, forecast] = weatherReport < span class = "string" > "Berkeley, CA"< / span >
2015-02-18 20:43:33 +00:00
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > city, forecast, ref, temp, weatherReport;
2011-09-21 03:42:19 +00:00
2016-09-22 20:19:43 +00:00
weatherReport = < span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > location< / span > ) < / span > {
2014-01-28 03:30:06 +00:00
< span class = "keyword" > return< / span > [location, < span class = "number" > 72< / span > , < span class = "string" > "Mostly Sunny"< / span > ];
2010-01-12 03:55:01 +00:00
};
2011-09-21 03:42:19 +00:00
2015-02-18 20:43:33 +00:00
ref = 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 > ];
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example28 = "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 > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example28);' > load< / div > < div class = "minibutton ok" onclick = "javascript: var city , forecast , ref , temp , weatherReport ;
2011-09-21 03:42:19 +00:00
2010-06-28 04:19:58 +00:00
weatherReport = function(location) {
2016-12-01 06:28:00 +00:00
return [location, 72, " Mostly Sunny" ];
2010-01-12 03:55:01 +00:00
};
2011-09-21 03:42:19 +00:00
2016-12-01 06:28:00 +00:00
ref = weatherReport(" Berkeley, CA" ), city = ref[0], temp = ref[1], forecast = ref[2];
;alert(forecast);">run: forecast< / div > < br class = 'clear' / > < / div >
2010-01-12 03:55:01 +00:00
< p >
2010-12-23 21:30:35 +00:00
Destructuring assignment can be used with any depth of array and object nesting,
2010-01-12 05:09:23 +00:00
to help pull out deeply nested properties.
2010-01-12 03:55:01 +00:00
< / p >
2014-01-28 01:56:29 +00:00
< div class = 'code' > < pre > < code > futurists =
2016-09-22 20:19:43 +00:00
sculptor: < span class = "string" > "Umberto Boccioni"< / span >
painter: < span class = "string" > "Vladimir Burliuk"< / span >
poet:
name: < span class = "string" > "F.T. Marinetti"< / span >
address: [
2014-01-28 03:30:06 +00:00
< span class = "string" > "Via Roma 42R"< / span >
< span class = "string" > "Bellagio, Italy 22021"< / span >
2010-01-12 03:55:01 +00:00
]
2016-09-22 20:19:43 +00:00
{poet: {name, address: [street, city]}} = futurists
2015-02-18 20:43:33 +00:00
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > city, futurists, name, ref, ref1, street;
2011-09-21 03:42:19 +00:00
2014-01-28 01:56:29 +00:00
futurists = {
2016-11-21 01:05:19 +00:00
< span class = "attr" > sculptor< / span > : < span class = "string" > "Umberto Boccioni"< / span > ,
< span class = "attr" > painter< / span > : < span class = "string" > "Vladimir Burliuk"< / span > ,
< span class = "attr" > poet< / span > : {
< span class = "attr" > name< / span > : < span class = "string" > "F.T. Marinetti"< / span > ,
< span class = "attr" > address< / span > : [< span class = "string" > "Via Roma 42R"< / span > , < span class = "string" > "Bellagio, Italy 22021"< / span > ]
2010-01-12 03:55:01 +00:00
}
};
2011-09-21 03:42:19 +00:00
2015-02-18 20:43:33 +00:00
ref = futurists.poet, name = ref.name, (ref1 = ref.address, street = ref1[< span class = "number" > 0< / span > ], city = ref1[< span class = "number" > 1< / span > ]);
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example29 = "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{poet: {name, address: [street, city]}} = futurists\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example29);' > load< / div > < div class = "minibutton ok" onclick = "javascript: var city , futurists , name , ref , ref1 , street ;
2011-09-21 03:42:19 +00:00
2010-01-12 03:55:01 +00:00
futurists = {
2016-12-01 06:28:00 +00:00
sculptor: " Umberto Boccioni" ,
painter: " Vladimir Burliuk" ,
2010-01-12 03:55:01 +00:00
poet: {
2016-12-01 06:28:00 +00:00
name: " F.T. Marinetti" ,
address: [" Via Roma 42R" , " Bellagio, Italy 22021" ]
2010-01-12 03:55:01 +00:00
}
};
2011-09-21 03:42:19 +00:00
2015-02-18 20:43:33 +00:00
ref = futurists.poet, name = ref.name, (ref1 = ref.address, street = ref1[0], city = ref1[1]);
2016-12-01 06:28:00 +00:00
;alert(name + " -" + street);">run: name + "-" + street< / div > < br class = 'clear' / > < / div >
2010-03-23 04:18:50 +00:00
< p >
2010-12-23 21:30:35 +00:00
Destructuring assignment can even be combined with splats.
2010-03-23 04:18:50 +00:00
< / p >
2014-01-28 03:30:06 +00:00
< div class = 'code' > < pre > < code > tag = < span class = "string" > "< impossible> "< / span >
2010-03-23 04:18:50 +00:00
2014-01-28 03:30:06 +00:00
[open, contents..., close] = tag.split(< span class = "string" > ""< / span > )
2015-02-18 20:43:33 +00:00
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > close, contents, i, open, ref, tag,
slice = [].slice;
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
tag = < span class = "string" > "< impossible> "< / span > ;
2011-09-21 03:42:19 +00:00
2015-02-18 20:43:33 +00:00
ref = 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++];
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example30 = "tag = \"<impossible>\"\n\n[open, contents..., close] = tag.split(\"\")\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example30);' > load< / div > < div class = "minibutton ok" onclick = "javascript: var close , contents , i , open , ref , tag ,
2015-02-18 20:43:33 +00:00
slice = [].slice;
2011-09-21 03:42:19 +00:00
2016-12-01 06:28:00 +00:00
tag = " < impossible > " ;
2011-09-21 03:42:19 +00:00
2016-12-01 06:28:00 +00:00
ref = tag.split(" " ), open = ref[0], contents = 3 < = ref.length ? slice.call(ref, 1, i = ref.length - 1) : (i = 1, []), close = ref[i++];
;alert(contents.join(" " ));">run: contents.join("")< / div > < br class = 'clear' / > < / div >
2014-01-28 01:56:29 +00:00
< p >
2014-01-28 03:30:06 +00:00
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.
2014-01-28 01:56:29 +00:00
< / p >
2014-01-28 03:30:06 +00:00
< div class = 'code' > < pre > < code > text = < span class = "string" > "Every literary critic believes he will
outwit history and have the last word"< / span >
[first, ..., last] = text.split < span class = "string" > " "< / span >
2015-02-18 20:43:33 +00:00
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > first, last, ref, text;
2014-01-28 03:30:06 +00:00
text = < span class = "string" > "Every literary critic believes he will outwit history and have the last word"< / span > ;
2015-02-18 20:43:33 +00:00
ref = text.split(< span class = "string" > " "< / span > ), first = ref[< span class = "number" > 0< / span > ], last = ref[ref.length - < span class = "number" > 1< / span > ];
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example31 = "text = \"Every literary critic believes he will\n outwit history and have the last word\"\n\n[first, ..., last] = text.split \" \"\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example31);' > load< / div > < div class = "minibutton ok" onclick = "javascript: var first , last , ref , text ;
2014-01-28 03:30:06 +00:00
2016-12-01 06:28:00 +00:00
text = " Every literary critic believes he will outwit history and have the last word" ;
2014-01-28 03:30:06 +00:00
2016-12-01 06:28:00 +00:00
ref = text.split(" " ), first = ref[0], last = ref[ref.length - 1];
;alert(first + " " + last);">run: first + " " + last< / div > < br class = 'clear' / > < / div >
2012-06-10 04:19:54 +00:00
< p >
2013-03-18 05:06:33 +00:00
Destructuring assignment is also useful when combined with class constructors
2013-06-02 05:37:45 +00:00
to assign properties to your instance from an options object passed to the constructor.
2012-06-10 04:19:54 +00:00
< / p >
2014-01-28 03:30:06 +00:00
< div class = 'code' > < pre > < code > < span class = "class" > < span class = "keyword" > class< / span > < span class = "title" > Person< / span > < / span >
2016-09-22 20:19:43 +00:00
constructor: < span class = "function" > < span class = "params" > (options)< / span > -> < / span >
{@name, @age, @height = < span class = "string" > 'average'< / span > } = options
2012-06-10 04:19:54 +00:00
2016-09-22 20:19:43 +00:00
tim = < span class = "keyword" > new< / span > Person name: < span class = "string" > 'Tim'< / span > , age: < span class = "number" > 4< / span >
2014-01-28 03:30:06 +00:00
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > Person, tim;
2014-01-28 01:56:29 +00:00
2016-09-22 20:19:43 +00:00
Person = (< span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > < / span > ) < / span > {
< span class = "function" > < span class = "keyword" > function< / span > < span class = "title" > Person< / span > (< span class = "params" > options< / span > ) < / span > {
2015-09-03 16:37:36 +00:00
< span class = "keyword" > var< / span > ref;
< 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 > ;
2012-06-10 04:19:54 +00:00
}
2014-01-28 03:30:06 +00:00
< span class = "keyword" > return< / span > Person;
2012-06-10 04:19:54 +00:00
})();
2014-01-28 01:56:29 +00:00
2014-01-28 03:30:06 +00:00
tim = < span class = "keyword" > new< / span > Person({
2016-11-21 01:05:19 +00:00
< span class = "attr" > name< / span > : < span class = "string" > 'Tim'< / span > ,
< span class = "attr" > age< / span > : < span class = "number" > 4< / span >
2014-01-28 01:56:29 +00:00
});
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example32 = "class Person\n constructor: (options) ->\n {@name, @age, @height = 'average'} = options\n\ntim = new Person name: 'Tim', age: 4\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example32);' > load< / div > < div class = "minibutton ok" onclick = "javascript: var Person , tim ;
2012-06-10 04:19:54 +00:00
Person = (function() {
function Person(options) {
2015-09-03 16:37:36 +00:00
var ref;
this.name = options.name, this.age = options.age, this.height = (ref = options.height) != null ? ref : 'average';
2012-06-10 04:19:54 +00:00
}
return Person;
})();
2014-01-28 01:56:29 +00:00
tim = new Person({
2015-09-03 16:37:36 +00:00
name: 'Tim',
2014-01-28 01:56:29 +00:00
age: 4
});
2016-12-01 06:28:00 +00:00
;alert(tim.age + " " + tim.height);">run: tim.age + " " + tim.height< / div > < br class = 'clear' / > < / div >
2015-09-03 16:37:36 +00:00
< 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 >
2010-01-12 03:55:01 +00:00
2010-02-21 19:30:21 +00:00
< p >
2013-03-04 19:19:08 +00:00
< span id = "fat-arrow" class = "bookmark" > < / span >
2015-01-29 17:25:25 +00:00
< b class = "header" > Bound Functions, Generator Functions< / b >
2015-09-03 16:37:36 +00:00
In JavaScript, the < code > this< / code > keyword is dynamically scoped to mean the
2010-03-07 00:29:41 +00:00
object that the current function is attached to. If you pass a function as
2015-09-03 16:37:36 +00:00
a callback or attach it to a different object, the original value of < code > this< / code >
2016-12-01 06:28:00 +00:00
will be lost. If you’ re not familiar with this behavior,
2016-09-22 20:19:43 +00:00
< a href = "http://64.13.255.16/articles/scope_in_javascript/" > this Digital Web article< / a >
2010-03-07 00:29:41 +00:00
gives a good overview of the quirks.
< / p >
< p >
2015-09-03 16:37:36 +00:00
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
2010-01-14 04:24:45 +00:00
when using callback-based libraries like Prototype or jQuery, for creating
2015-09-03 16:37:36 +00:00
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
2016-12-01 06:28:00 +00:00
properties of the < code > this< / code > where they’ re defined.
2010-01-14 04:24:45 +00:00
< / p >
2014-01-28 03:30:06 +00:00
< div class = 'code' > < pre > < code > < span class = "function" > < span class = "title" > Account< / span > = < span class = "params" > (customer, cart)< / span > -> < / span >
2016-09-22 20:19:43 +00:00
@customer = customer
@cart = cart
2014-01-28 01:56:29 +00:00
2016-09-22 20:19:43 +00:00
$(< 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 >
@customer.purchase @cart
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > Account;
2014-01-28 01:56:29 +00:00
2016-09-22 20:19:43 +00:00
Account = < span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > customer, cart< / span > ) < / span > {
2014-01-28 03:30:06 +00:00
< span class = "keyword" > this< / span > .customer = customer;
< span class = "keyword" > this< / span > .cart = cart;
2016-09-22 20:19:43 +00:00
< 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 > {
< span class = "keyword" > return< / span > < span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > event< / span > ) < / span > {
2014-01-28 03:30:06 +00:00
< span class = "keyword" > return< / span > _this.customer.purchase(_this.cart);
2014-01-28 01:56:29 +00:00
};
2014-01-28 03:30:06 +00:00
})(< span class = "keyword" > this< / span > ));
2010-01-14 04:24:45 +00:00
};
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example33 = "Account = (customer, cart) ->\n @customer = customer\n @cart = cart\n\n $('.shopping_cart').on 'click', (event) =>\n @customer.purchase @cart\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example33);' > load< / div > < br class = 'clear' / > < / div >
2010-03-07 00:29:41 +00:00
< p >
2015-09-03 16:37:36 +00:00
If we had used < code > -> < / code > in the callback above, < code > @customer< / code > would
2016-12-01 06:28:00 +00:00
have referred to the undefined “customer” property of the DOM element,
2015-09-03 16:37:36 +00:00
and trying to call < code > purchase()< / code > on it would have raised an exception.
2010-03-07 00:29:41 +00:00
< / p >
2011-12-18 18:41:33 +00:00
< 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 >
2015-01-29 17:25:25 +00:00
< p >
2016-12-01 06:28:00 +00:00
< span id = "generator-functions" class = "bookmark" > < / span >
2015-04-15 15:26:30 +00:00
CoffeeScript functions also support
2016-12-01 06:28:00 +00:00
< 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.
2015-01-29 17:25:25 +00:00
< / p >
< div class = 'code' > < pre > < code > < span class = "function" > < span class = "title" > perfectSquares< / span > = -> < / span >
num = < span class = "number" > 0< / span >
< span class = "keyword" > loop< / span >
num += < span class = "number" > 1< / span >
2016-11-21 01:05:19 +00:00
< span class = "keyword" > yield< / span > num * num
2015-01-29 17:25:25 +00:00
< span class = "keyword" > return< / span >
2016-09-22 20:19:43 +00:00
< span class = "built_in" > window< / span > .ps < span class = "keyword" > or< / span > = perfectSquares()
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > perfectSquares;
2015-01-29 17:25:25 +00:00
2016-09-22 20:19:43 +00:00
perfectSquares = < span class = "function" > < span class = "keyword" > function< / span > *(< span class = "params" > < / span > ) < / span > {
2015-01-29 17:25:25 +00:00
< span class = "keyword" > var< / span > num;
num = < span class = "number" > 0< / span > ;
< span class = "keyword" > while< / span > (< span class = "literal" > true< / span > ) {
num += < span class = "number" > 1< / span > ;
2016-09-22 20:19:43 +00:00
< span class = "keyword" > yield< / span > num * num;
2015-01-29 17:25:25 +00:00
}
};
2016-09-22 20:19:43 +00:00
< span class = "built_in" > window< / span > .ps || (< span class = "built_in" > window< / span > .ps = perfectSquares());
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example34 = "perfectSquares = ->\n num = 0\n loop\n num += 1\n yield num * num\n return\n\nwindow.ps or= perfectSquares()\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example34);' > load< / div > < div class = "minibutton ok" onclick = "javascript: var perfectSquares ;
2015-01-29 17:25:25 +00:00
perfectSquares = function*() {
var num;
num = 0;
while (true) {
num += 1;
2016-09-22 20:19:43 +00:00
yield num * num;
2015-01-29 17:25:25 +00:00
}
};
window.ps || (window.ps = perfectSquares());
2016-12-01 06:28:00 +00:00
;alert(ps.next().value);">run: ps.next().value< / div > < br class = 'clear' / > < / div >
2015-05-14 09:23:10 +00:00
< p >
2015-09-03 16:37:36 +00:00
< code > yield*< / code > is called < code > yield from< / code > , and < code > yield return< / code >
2016-12-01 06:28:00 +00:00
may be used if you need to force a generator that doesn’ t yield.
< / p >
< p >
< span id = "generator-iteration" class = "bookmark" > < / span >
You can iterate over a generator function using < code > for…from< / code > .
2015-05-14 09:23:10 +00:00
< / p >
2016-12-01 06:28:00 +00:00
< div class = 'code' > < pre > < code > < span class = "function" > < span class = "title" > fibonacci< / span > = -> < / span >
[previous, current] = [< span class = "number" > 1< / span > , < span class = "number" > 1< / span > ]
< span class = "keyword" > loop< / span >
[previous, current] = [current, previous + current]
< span class = "keyword" > yield< / span > current
< span class = "keyword" > return< / span >
< span class = "function" >
< span class = "title" > getFibonacciNumbers< / span > = < span class = "params" > (length)< / span > -> < / span >
results = [< span class = "number" > 1< / span > ]
< span class = "keyword" > for< / span > n < span class = "keyword" > from< / span > fibonacci()
results.push n
< span class = "keyword" > break< / span > < span class = "keyword" > if< / span > results.length < span class = "keyword" > is< / span > length
results
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > fibonacci, getFibonacciNumbers;
fibonacci = < span class = "function" > < span class = "keyword" > function< / span > *(< span class = "params" > < / span > ) < / span > {
< span class = "keyword" > var< / span > current, previous, ref, ref1;
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 > ];
< span class = "keyword" > while< / span > (< span class = "literal" > true< / span > ) {
ref1 = [current, previous + current], previous = ref1[< span class = "number" > 0< / span > ], current = ref1[< span class = "number" > 1< / span > ];
< span class = "keyword" > yield< / span > current;
}
};
getFibonacciNumbers = < span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > length< / span > ) < / span > {
< span class = "keyword" > var< / span > n, ref, results;
results = [< span class = "number" > 1< / span > ];
ref = fibonacci();
< span class = "keyword" > for< / span > (n < span class = "keyword" > of< / span > ref) {
results.push(n);
< span class = "keyword" > if< / span > (results.length === length) {
< span class = "keyword" > break< / span > ;
}
}
< span class = "keyword" > return< / span > results;
};
< / code > < / pre > < script > window . example35 = "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 > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example35);' > load< / div > < div class = "minibutton ok" onclick = "javascript: var fibonacci , getFibonacciNumbers ;
fibonacci = function*() {
var current, previous, ref, ref1;
ref = [1, 1], previous = ref[0], current = ref[1];
while (true) {
ref1 = [current, previous + current], previous = ref1[0], current = ref1[1];
yield current;
}
};
getFibonacciNumbers = function(length) {
var n, ref, results;
results = [1];
ref = fibonacci();
for (n of ref) {
results.push(n);
if (results.length === length) {
break;
}
}
return results;
};
;alert(getFibonacciNumbers(10));">run: getFibonacciNumbers(10)< / div > < br class = 'clear' / > < / div >
2010-01-14 04:24:45 +00:00
2010-02-21 19:30:21 +00:00
< p >
< span id = "embedded" class = "bookmark" > < / span >
2009-12-24 00:42:18 +00:00
< b class = "header" > Embedded JavaScript< / b >
2016-12-01 06:28:00 +00:00
Hopefully, you’ ll never need to use it, but if you ever need to intersperse
2010-01-11 14:09:06 +00:00
snippets of JavaScript within your CoffeeScript, you can
use backticks to pass it straight through.
2009-12-24 00:42:18 +00:00
< / p >
2016-09-22 20:19:43 +00:00
< div class = 'code' > < pre > < code > hi = `< span class = "javascript" > < span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > < / span > ) < / span > {
< span class = "keyword" > return< / span > [< span class = "built_in" > document< / span > .title, < span class = "string" > "Hello JavaScript"< / span > ].join(< span class = "string" > ": "< / span > );
2014-01-28 01:56:29 +00:00
}< / span > `
2014-01-28 03:30:06 +00:00
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > hi;
2011-09-21 03:42:19 +00:00
2016-09-22 20:19:43 +00:00
hi = < span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > < / span > ) < / span > {
< span class = "keyword" > return< / span > [< span class = "built_in" > document< / span > .title, < span class = "string" > "Hello JavaScript"< / span > ].join(< span class = "string" > ": "< / span > );
2009-12-21 16:41:45 +00:00
};
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example36 = "hi = `function() {\n return [document.title, \"Hello JavaScript\"].join(\": \");\n}`\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example36);' > load< / div > < div class = "minibutton ok" onclick = "javascript: var hi ;
2011-09-21 03:42:19 +00:00
2009-12-26 07:17:34 +00:00
hi = function() {
2016-12-01 06:28:00 +00:00
return [document.title, " Hello JavaScript" ].join(" : " );
};
;alert(hi());">run: hi()< / div > < br class = 'clear' / > < / div >
< p >
Escape backticks with backslashes: < code > \`< / code > becomes < code > `< / code > .
< / p >
< p >
Escape backslashes before backticks with more backslashes: < code > \\\`< / code >
becomes < code > \`< / code > .
< / p >
< div class = 'code' > < pre > < code > markdown = `< span class = "javascript" > < span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > < / span > ) < / span > {
< span class = "keyword" > return< / span > \< / span > `In Markdown, write code like \\\`< span class = "javascript" > < span class = "keyword" > this< / span > \\\< / span > `\`< span class = "javascript" > ;
}< / span > `
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > markdown;
markdown = < span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > < / span > ) < / span > {
< span class = "keyword" > return< / span > < span class = "string" > `In Markdown, write code like \`this\``< / span > ;
2011-09-21 03:42:19 +00:00
};
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example37 = "markdown = `function () {\n return \\`In Markdown, write code like \\\\\\`this\\\\\\`\\`;\n}`\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example37);' > load< / div > < div class = "minibutton ok" onclick = "javascript: var markdown ;
markdown = function () {
return `In Markdown, write code like \`this\``;
};
;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 >
< div class = 'code' > < pre > < code > ```< span class = "javascript" >
< span class = "function" > < span class = "keyword" > function< / span > < span class = "title" > time< / span > (< span class = "params" > < / span > ) < / span > {
< 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 > ;
}
< / span > ```
< / code > < / pre > < pre > < code >
< span class = "function" > < span class = "keyword" > function< / span > < span class = "title" > time< / span > (< span class = "params" > < / span > ) < / span > {
< 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 > ;
}
;
< / code > < / pre > < script > window . example38 = "```\nfunction time() {\n return `The time is ${new Date().toLocaleTimeString()}`;\n}\n```\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example38);' > load< / div > < div class = "minibutton ok" onclick = "javascript:
function time() {
return `The time is ${new Date().toLocaleTimeString()}`;
}
;
;alert(time());">run: time()< / div > < br class = 'clear' / > < / div >
2009-12-21 16:41:45 +00:00
2010-02-21 19:30:21 +00:00
< p >
< span id = "switch" class = "bookmark" > < / span >
2009-12-24 09:33:59 +00:00
< b class = "header" > Switch/When/Else< / b >
2010-09-27 19:21:45 +00:00
< b > Switch< / b > statements in JavaScript are a bit awkward. You need to
remember to < b > break< / b > at the end of every < b > case< / b > statement to
avoid accidentally falling through to the default case.
2015-09-03 16:37:36 +00:00
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.
2009-12-24 00:42:18 +00:00
< / p >
2010-01-14 04:24:45 +00:00
< p >
As in Ruby, < b > switch< / b > statements in CoffeeScript can take multiple
values for each < b > when< / b > clause. If any of the values match, the clause
runs.
< / p >
2014-01-28 03:30:06 +00:00
< div class = 'code' > < pre > < code > < span class = "keyword" > switch< / span > day
< span class = "keyword" > when< / span > < span class = "string" > "Mon"< / span > < span class = "keyword" > then< / span > go work
< span class = "keyword" > when< / span > < span class = "string" > "Tue"< / span > < span class = "keyword" > then< / span > go relax
< span class = "keyword" > when< / span > < span class = "string" > "Thu"< / span > < span class = "keyword" > then< / span > go iceFishing
< span class = "keyword" > when< / span > < span class = "string" > "Fri"< / span > , < span class = "string" > "Sat"< / span >
< span class = "keyword" > if< / span > day < span class = "keyword" > is< / span > bingoDay
2010-09-16 04:32:57 +00:00
go bingo
go dancing
2014-01-28 03:30:06 +00:00
< span class = "keyword" > when< / span > < span class = "string" > "Sun"< / span > < span class = "keyword" > then< / span > go church
2016-09-22 20:19:43 +00:00
< span class = "keyword" > else< / span > go work
< / code > < / pre > < pre > < code > < span class = "keyword" > switch< / span > (day) {
2014-01-28 03:30:06 +00:00
< span class = "keyword" > case< / span > < span class = "string" > "Mon"< / span > :
2014-01-28 01:56:29 +00:00
go(work);
2014-01-28 03:30:06 +00:00
< span class = "keyword" > break< / span > ;
< span class = "keyword" > case< / span > < span class = "string" > "Tue"< / span > :
2014-01-28 01:56:29 +00:00
go(relax);
2014-01-28 03:30:06 +00:00
< span class = "keyword" > break< / span > ;
< span class = "keyword" > case< / span > < span class = "string" > "Thu"< / span > :
2014-01-28 01:56:29 +00:00
go(iceFishing);
2014-01-28 03:30:06 +00:00
< span class = "keyword" > break< / span > ;
< span class = "keyword" > case< / span > < span class = "string" > "Fri"< / span > :
< span class = "keyword" > case< / span > < span class = "string" > "Sat"< / span > :
< span class = "keyword" > if< / span > (day === bingoDay) {
2014-01-28 01:56:29 +00:00
go(bingo);
go(dancing);
2010-09-22 03:58:05 +00:00
}
2014-01-28 03:30:06 +00:00
< span class = "keyword" > break< / span > ;
< span class = "keyword" > case< / span > < span class = "string" > "Sun"< / span > :
2014-01-28 01:56:29 +00:00
go(church);
2014-01-28 03:30:06 +00:00
< span class = "keyword" > break< / span > ;
< span class = "keyword" > default< / span > :
2014-01-28 01:56:29 +00:00
go(work);
2009-12-21 16:41:45 +00:00
}
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example39 = "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 > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example39);' > load< / div > < br class = 'clear' / > < / div >
2013-03-18 05:06:33 +00:00
< p >
Switch statements can also be used without a control expression, turning them in to a cleaner alternative to if/else chains.
< / p >
2014-01-28 03:30:06 +00:00
< div class = 'code' > < pre > < code > score = < span class = "number" > 76< / span >
grade = < span class = "keyword" > switch< / span >
< span class = "keyword" > when< / span > score < < span class = "number" > 60< / span > < span class = "keyword" > then< / span > < span class = "string" > 'F'< / span >
< span class = "keyword" > when< / span > score < < span class = "number" > 70< / span > < span class = "keyword" > then< / span > < span class = "string" > 'D'< / span >
< span class = "keyword" > when< / span > score < < span class = "number" > 80< / span > < span class = "keyword" > then< / span > < span class = "string" > 'C'< / span >
< span class = "keyword" > when< / span > score < < span class = "number" > 90< / span > < span class = "keyword" > then< / span > < span class = "string" > 'B'< / span >
< span class = "keyword" > else< / span > < span class = "string" > 'A'< / span >
< span class = "comment" > # grade == 'C'< / span >
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > grade, score;
score = < span class = "number" > 76< / span > ;
2016-09-22 20:19:43 +00:00
grade = (< span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > < / span > ) < / span > {
2014-01-28 03:30:06 +00:00
< span class = "keyword" > switch< / span > (< span class = "literal" > false< / span > ) {
< span class = "keyword" > case< / span > !(score < < span class = "number" > 60< / span > ):
< span class = "keyword" > return< / span > < span class = "string" > 'F'< / span > ;
< span class = "keyword" > case< / span > !(score < < span class = "number" > 70< / span > ):
< span class = "keyword" > return< / span > < span class = "string" > 'D'< / span > ;
< span class = "keyword" > case< / span > !(score < < span class = "number" > 80< / span > ):
< span class = "keyword" > return< / span > < span class = "string" > 'C'< / span > ;
< span class = "keyword" > case< / span > !(score < < span class = "number" > 90< / span > ):
< span class = "keyword" > return< / span > < span class = "string" > 'B'< / span > ;
< span class = "keyword" > default< / span > :
< span class = "keyword" > return< / span > < span class = "string" > 'A'< / span > ;
2013-03-18 05:06:33 +00:00
}
})();
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example40 = "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 > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example40);' > load< / div > < br class = 'clear' / > < / div >
2009-12-21 16:41:45 +00:00
2010-02-21 19:30:21 +00:00
< p >
< span id = "try" class = "bookmark" > < / span >
2009-12-24 00:42:18 +00:00
< b class = "header" > Try/Catch/Finally< / b >
2015-05-14 09:23:10 +00:00
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.
2009-12-24 00:42:18 +00:00
< / p >
2014-01-28 03:30:06 +00:00
< div class = 'code' > < pre > < code > < span class = "keyword" > try< / span >
2010-06-28 04:19:58 +00:00
allHellBreaksLoose()
catsAndDogsLivingTogether()
2014-01-28 03:30:06 +00:00
< span class = "keyword" > catch< / span > error
< span class = "built_in" > print< / span > error
< span class = "keyword" > finally< / span >
2010-06-28 04:19:58 +00:00
cleanUp()
2016-09-22 20:19:43 +00:00
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > error;
2013-03-18 05:06:33 +00:00
2014-01-28 03:30:06 +00:00
< span class = "keyword" > try< / span > {
2010-06-28 04:19:58 +00:00
allHellBreaksLoose();
catsAndDogsLivingTogether();
2015-09-03 16:37:36 +00:00
} < span class = "keyword" > catch< / span > (error1) {
error = error1;
2014-01-28 01:56:29 +00:00
print(error);
2014-01-28 03:30:06 +00:00
} < span class = "keyword" > finally< / span > {
2010-06-28 04:19:58 +00:00
cleanUp();
2009-12-21 16:41:45 +00:00
}
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example41 = "try\n allHellBreaksLoose()\n catsAndDogsLivingTogether()\ncatch error\n print error\nfinally\n cleanUp()\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example41);' > load< / div > < br class = 'clear' / > < / div >
2009-12-21 16:41:45 +00:00
2010-02-21 19:30:21 +00:00
< p >
< span id = "comparisons" class = "bookmark" > < / span >
2010-01-17 23:12:59 +00:00
< b class = "header" > Chained Comparisons< / b >
CoffeeScript borrows
< a href = "http://docs.python.org/reference/expressions.html#notin" > chained comparisons< / a >
2016-12-01 06:28:00 +00:00
from Python — making it easy to test if a value falls within a
2010-01-17 23:12:59 +00:00
certain range.
< / p >
2014-01-28 03:30:06 +00:00
< div class = 'code' > < pre > < code > cholesterol = < span class = "number" > 127< / span >
2010-01-17 23:12:59 +00:00
2014-01-28 03:30:06 +00:00
healthy = < span class = "number" > 200< / span > > cholesterol > < span class = "number" > 60< / span >
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > cholesterol, healthy;
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
cholesterol = < span class = "number" > 127< / span > ;
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
healthy = (< span class = "number" > 200< / span > > cholesterol & & cholesterol > < span class = "number" > 60< / span > );
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example42 = "cholesterol = 127\n\nhealthy = 200 > cholesterol > 60\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example42);' > load< / div > < div class = "minibutton ok" onclick = "javascript: var cholesterol , healthy ;
2011-09-21 03:42:19 +00:00
2010-01-17 23:12:59 +00:00
cholesterol = 127;
2011-09-21 03:42:19 +00:00
healthy = (200 > cholesterol & & cholesterol > 60);
2016-12-01 06:28:00 +00:00
;alert(healthy);">run: healthy< / div > < br class = 'clear' / > < / div >
2010-01-17 23:12:59 +00:00
2010-03-23 04:18:50 +00:00
< p >
2010-11-22 02:12:59 +00:00
< span id = "strings" class = "bookmark" > < / span >
2012-04-25 18:37:05 +00:00
< b class = "header" > String Interpolation, Block Strings, and Block Comments< / b >
2010-08-24 02:08:33 +00:00
Ruby-style string interpolation is included in CoffeeScript. Double-quoted
2016-12-01 06:28:00 +00:00
strings allow for interpolated values, using < code > #{ … }< / code > ,
2015-05-14 09:23:10 +00:00
and single-quoted strings are literal. You may even use interpolation in
object keys.
2010-03-23 04:18:50 +00:00
< / p >
2014-01-28 03:30:06 +00:00
< div class = 'code' > < pre > < code > author = < span class = "string" > "Wittgenstein"< / span >
quote = < span class = "string" > "A picture is a fact. -- < span class = "subst" > #{ author }< / span > "< / span >
2010-03-23 04:18:50 +00:00
2014-01-28 03:30:06 +00:00
sentence = < span class = "string" > "< span class = "subst" > #{ < span class = "number" > 22< / span > / < span class = "number" > 7< / span > }< / span > is a decent approximation of π"< / span >
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > author, quote, sentence;
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
author = < span class = "string" > "Wittgenstein"< / span > ;
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
quote = < span class = "string" > "A picture is a fact. -- "< / span > + author;
2011-09-21 03:42:19 +00:00
2015-01-29 17:20:46 +00:00
sentence = (< span class = "number" > 22< / span > / < span class = "number" > 7< / span > ) + < span class = "string" > " is a decent approximation of π"< / span > ;
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example43 = "author = \"Wittgenstein\"\nquote = \"A picture is a fact. -- #{ author }\"\n\nsentence = \"#{ 22 / 7 } is a decent approximation of π\"\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example43);' > load< / div > < div class = "minibutton ok" onclick = "javascript: var author , quote , sentence ;
2011-09-21 03:42:19 +00:00
2016-12-01 06:28:00 +00:00
author = " Wittgenstein" ;
2011-09-21 03:42:19 +00:00
2016-12-01 06:28:00 +00:00
quote = " A picture is a fact. -- " + author;
2011-09-21 03:42:19 +00:00
2016-12-01 06:28:00 +00:00
sentence = (22 / 7) + " is a decent approximation of π" ;
;alert(sentence);">run: sentence< / div > < br class = 'clear' / > < / div >
2010-02-21 19:30:21 +00:00
< p >
2014-01-28 01:56:29 +00:00
Multiline strings are allowed in CoffeeScript. Lines are joined by a single space unless they end with a backslash. Indentation is ignored.
2009-12-24 00:42:18 +00:00
< / p >
2014-01-28 03:30:06 +00:00
< div class = 'code' > < pre > < code > mobyDick = < span class = "string" > "Call me Ishmael. Some years ago --
2014-01-28 01:56:29 +00:00
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 >
2014-01-28 03:30:06 +00:00
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > mobyDick;
2009-12-24 06:22:41 +00:00
2014-01-28 03:30:06 +00:00
mobyDick = < 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 > ;
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example44 = "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 > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example44);' > load< / div > < div class = "minibutton ok" onclick = "javascript: var mobyDick ;
2011-09-21 03:42:19 +00:00
2016-12-01 06:28:00 +00:00
mobyDick = " 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..." ;
;alert(mobyDick);">run: mobyDick< / div > < br class = 'clear' / > < / div >
2010-01-12 05:00:38 +00:00
< p >
2012-04-25 18:37:05 +00:00
Block strings can be used to hold formatted or indentation-sensitive text
2016-12-01 06:28:00 +00:00
(or, if you just don’ t feel like escaping quotes and apostrophes). The
2012-04-25 18:37:05 +00:00
indentation level that begins the block is maintained throughout, so
2010-01-12 05:00:38 +00:00
you can keep it all aligned with the body of your code.
< / p >
2014-01-28 03:30:06 +00:00
< div class = 'code' > < pre > < code > html = < span class = "string" > """
2014-01-28 01:56:29 +00:00
< strong>
cup of coffeescript
< /strong>
"""< / span >
2014-01-28 03:30:06 +00:00
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > html;
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
html = < span class = "string" > "< strong> \n cup of coffeescript\n< /strong> "< / span > ;
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example45 = "html = \"\"\"\n <strong>\n cup of coffeescript\n </strong>\n \"\"\"\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example45);' > load< / div > < div class = "minibutton ok" onclick = "javascript: var html ;
2011-09-21 03:42:19 +00:00
2016-12-01 06:28:00 +00:00
html = " < strong > \n cup of coffeescript\n< / strong > " ;
;alert(html);">run: html< / div > < br class = 'clear' / > < / div >
2010-03-08 11:34:07 +00:00
< p >
2012-04-25 18:37:05 +00:00
Double-quoted block strings, like other double-quoted strings, allow interpolation.
2010-03-08 11:34:07 +00:00
< / p >
2010-07-17 14:28:06 +00:00
< p >
2016-12-01 06:28:00 +00:00
Sometimes you’ d like to pass a block comment through to the generated
2010-07-29 04:51:35 +00:00
JavaScript. For example, when you need to embed a licensing header at
2012-04-25 18:37:05 +00:00
the top of a file. Block comments, which mirror the syntax for block strings,
2010-07-17 14:28:06 +00:00
are preserved in the generated code.
< / p >
2014-01-28 03:30:06 +00:00
< div class = 'code' > < pre > < code > < span class = "comment" > ###
2014-01-28 01:56:29 +00:00
SkinnyMochaHalfCaffScript Compiler v1.0
Released under the MIT License
###< / span >
< / code > < / pre > < pre > < code >
2014-01-28 03:30:06 +00:00
< span class = "comment" > /*
2014-01-28 01:56:29 +00:00
SkinnyMochaHalfCaffScript Compiler v1.0
Released under the MIT License
*/< / span >
2011-09-21 03:42:19 +00:00
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example46 = "###\nSkinnyMochaHalfCaffScript Compiler v1.0\nReleased under the MIT License\n###\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example46);' > load< / div > < br class = 'clear' / > < / div >
< p >
< span id = "tagged-template-literals" class = "bookmark" > < / span >
< b class = "header" > Tagged Template Literals< / b >
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 >
< 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 >
< div class = 'code' > < pre > < code > < span class = "function" > < span class = "title" > upperCaseExpr< / span > = < span class = "params" > (textParts, expressions...)< / span > -> < / span >
textParts.reduce (text, textPart, i) ->
text + expressions[i - < span class = "number" > 1< / span > ].toUpperCase() + textPart
< span class = "function" >
< span class = "title" > greet< / span > = < span class = "params" > (name, adjective)< / span > -> < / span >
upperCaseExpr< span class = "string" > """
Hi < span class = "subst" > #{name}< / span > . You look < span class = "subst" > #{adjective}< / span > !
"""< / span >
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > greet, upperCaseExpr,
slice = [].slice;
upperCaseExpr = < span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > < / span > ) < / span > {
< span class = "keyword" > var< / span > expressions, textParts;
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 > ) : [];
< span class = "keyword" > return< / span > textParts.reduce(< span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > text, textPart, i< / span > ) < / span > {
< span class = "keyword" > return< / span > text + expressions[i - < span class = "number" > 1< / span > ].toUpperCase() + textPart;
});
};
greet = < span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > name, adjective< / span > ) < / span > {
< span class = "keyword" > return< / span > upperCaseExpr< span class = "string" > `Hi < span class = "subst" > ${name}< / span > . You look < span class = "subst" > ${adjective}< / span > !`< / span > ;
};
< / code > < / pre > < script > window . example47 = "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 > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example47);' > load< / div > < div class = "minibutton ok" onclick = "javascript: var greet , upperCaseExpr ,
slice = [].slice;
upperCaseExpr = function() {
var expressions, textParts;
textParts = arguments[0], expressions = 2 < = arguments.length ? slice.call(arguments, 1) : [];
return textParts.reduce(function(text, textPart, i) {
return text + expressions[i - 1].toUpperCase() + textPart;
});
};
greet = function(name, adjective) {
return upperCaseExpr`Hi ${name}. You look ${adjective}!`;
};
;alert(greet(" greg" , " awesome" ));">run: greet("greg", "awesome")< / div > < br class = 'clear' / > < / div >
2010-03-08 11:34:07 +00:00
2010-11-22 02:12:59 +00:00
< p >
< span id = "regexes" class = "bookmark" > < / span >
2012-04-25 18:37:05 +00:00
< b class = "header" > Block Regular Expressions< / b >
2016-12-01 06:28:00 +00:00
Similar to block strings and comments, CoffeeScript supports block regexes —
2010-11-22 02:12:59 +00:00
extended regular expressions that ignore internal whitespace and can contain
2016-12-01 06:28:00 +00:00
comments and interpolation. Modeled after Perl’ s < code > /x< / code > modifier, CoffeeScript’ s
2015-09-03 16:37:36 +00:00
block regexes are delimited by < code > ///< / code > and go a long way towards making complex
2012-04-10 18:57:45 +00:00
regular expressions readable. To quote from the CoffeeScript source:
2010-11-22 02:12:59 +00:00
< / p >
2014-01-28 03:30:06 +00:00
< div class = 'code' > < pre > < code > OPERATOR = < span class = "regexp" > /// ^ (
?: [-=]> < span class = "comment" > # function< / span >
| [-+*/%< > & |^!?=]= < span class = "comment" > # compound assign / compare< / span >
| > > > =? < span class = "comment" > # zero-fill right shift< / span >
| ([-+:])\1 < span class = "comment" > # doubles< / span >
| ([& |< > ])\2=? < span class = "comment" > # logic / shift< / span >
| \?\. < span class = "comment" > # soak access< / span >
| \.{2,3} < span class = "comment" > # range or splat< / span >
2014-01-28 01:56:29 +00:00
) ///< / span >
2014-01-28 03:30:06 +00:00
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > OPERATOR;
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
OPERATOR = < span class = "regexp" > /^(?:[-=]> |[-+*\/%< > & |^!?=]=|> > > =?|([-+:])\1|([& |< > ])\2=?|\?\.|\.{2,3})/< / span > ;
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example48 = "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 > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example48);' > load< / div > < br class = 'clear' / > < / div >
2010-11-22 02:12:59 +00:00
2016-09-22 20:19:43 +00:00
< p >
< span id = "modules" class = "bookmark" > < / span >
< b class = "header" > Modules< / b >
ES2015 modules are supported in CoffeeScript, with very similar < code > import< / code >
and < code > export< / code > syntax:
< / p >
2016-11-21 01:05:19 +00:00
< div class = 'code' > < pre > < code > < span class = "keyword" > import< / span > < span class = "string" > 'local-file.coffee'< / span >
< span class = "keyword" > import< / span > < span class = "string" > 'coffee-script'< / span >
2016-09-22 20:19:43 +00:00
2016-11-21 01:05:19 +00:00
< span class = "keyword" > import< / span > _ < span class = "keyword" > from< / span > < span class = "string" > 'underscore'< / span >
< span class = "keyword" > import< / span > * < span class = "keyword" > as< / span > underscore < span class = "keyword" > from< / span > < span class = "string" > 'underscore'< / span >
2016-09-22 20:19:43 +00:00
2016-11-21 01:05:19 +00:00
< span class = "keyword" > import< / span > { now } < span class = "keyword" > from< / span > < span class = "string" > 'underscore'< / span >
< span class = "keyword" > import< / span > { now < span class = "keyword" > as< / span > currentTimestamp } < span class = "keyword" > from< / span > < span class = "string" > 'underscore'< / span >
< span class = "keyword" > import< / span > { first, last } < span class = "keyword" > from< / span > < span class = "string" > 'underscore'< / span >
< span class = "keyword" > import< / span > utilityBelt, { each } < span class = "keyword" > from< / span > < span class = "string" > 'underscore'< / span >
2016-09-22 20:19:43 +00:00
2016-11-21 01:05:19 +00:00
< span class = "keyword" > export< / span > < span class = "keyword" > default< / span > Math
< span class = "keyword" > export< / span > square = < span class = "function" > < span class = "params" > (x)< / span > -> < / span > x * x
< span class = "keyword" > export< / span > < span class = "class" > < span class = "keyword" > class< / span > < span class = "title" > Mathematics< / span > < / span >
2016-09-22 20:19:43 +00:00
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
2016-11-21 01:05:19 +00:00
< span class = "keyword" > export< / span > { sqrt }
< span class = "keyword" > export< / span > { sqrt < span class = "keyword" > as< / span > squareRoot }
2016-12-01 06:28:00 +00:00
< span class = "keyword" > export< / span > { Mathematics < span class = "keyword" > as< / span > < span class = "keyword" > default< / span > , sqrt < span class = "keyword" > as< / span > squareRoot }
2016-09-22 20:19:43 +00:00
2016-11-21 01:05:19 +00:00
< span class = "keyword" > export< / span > * < span class = "keyword" > from< / span > < span class = "string" > 'underscore'< / span >
< span class = "keyword" > export< / span > { max, min } < span class = "keyword" > from< / span > < span class = "string" > 'underscore'< / span >
2016-09-22 20:19:43 +00:00
< / code > < / pre > < pre > < code > < span class = "keyword" > import< / span > < span class = "string" > 'local-file.coffee'< / span > ;
< span class = "keyword" > import< / span > < span class = "string" > 'coffee-script'< / span > ;
< span class = "keyword" > import< / span > _ < span class = "keyword" > from< / span > < span class = "string" > 'underscore'< / span > ;
< span class = "keyword" > import< / span > * < span class = "keyword" > as< / span > underscore < span class = "keyword" > from< / span > < span class = "string" > 'underscore'< / span > ;
< span class = "keyword" > import< / span > {
now
} < span class = "keyword" > from< / span > < span class = "string" > 'underscore'< / span > ;
< span class = "keyword" > import< / span > {
now < span class = "keyword" > as< / span > currentTimestamp
} < span class = "keyword" > from< / span > < span class = "string" > 'underscore'< / span > ;
< span class = "keyword" > import< / span > {
first,
last
} < span class = "keyword" > from< / span > < span class = "string" > 'underscore'< / span > ;
< span class = "keyword" > import< / span > utilityBelt, {
each
} < span class = "keyword" > from< / span > < span class = "string" > 'underscore'< / span > ;
< span class = "keyword" > export< / span > < span class = "keyword" > default< / span > < span class = "built_in" > Math< / span > ;
< span class = "keyword" > export< / span > < span class = "keyword" > var< / span > square = < span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > x< / span > ) < / span > {
< span class = "keyword" > return< / span > x * x;
};
< span class = "keyword" > export< / span > < span class = "keyword" > var< / span > Mathematics = (< span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > < / span > ) < / span > {
< span class = "function" > < span class = "keyword" > function< / span > < span class = "title" > Mathematics< / span > (< span class = "params" > < / span > ) < / span > {}
Mathematics.prototype.least = < span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > x, y< / span > ) < / span > {
< span class = "keyword" > if< / span > (x < y) {
< span class = "keyword" > return< / span > x;
} < span class = "keyword" > else< / span > {
< span class = "keyword" > return< / span > y;
}
};
< span class = "keyword" > return< / span > Mathematics;
})();
< span class = "keyword" > export< / span > {
sqrt
};
< span class = "keyword" > export< / span > {
sqrt < span class = "keyword" > as< / span > squareRoot
};
< span class = "keyword" > export< / span > {
Mathematics < span class = "keyword" > as< / span > < span class = "keyword" > default< / span > ,
sqrt < span class = "keyword" > as< / span > squareRoot
};
< span class = "keyword" > export< / span > * < span class = "keyword" > from< / span > < span class = "string" > 'underscore'< / span > ;
< span class = "keyword" > export< / span > {
max,
min
} < span class = "keyword" > from< / span > < span class = "string" > 'underscore'< / span > ;
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example49 = "import 'local-file.coffee'\nimport 'coffee-script'\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 > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example49);' > load< / div > < br class = 'clear' / > < / div >
2016-09-22 20:19:43 +00:00
< p >
2016-12-01 06:28:00 +00:00
< span id = "modules-note" class = "bookmark" > < / span >
2016-09-22 20:19:43 +00:00
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 >
< 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.
2010-11-22 02:12:59 +00:00
2010-02-21 23:37:52 +00:00
< h2 >
< span id = "cake" class = "bookmark" > < / span >
Cake, and Cakefiles
< / h2 >
< p >
2011-08-14 20:24:26 +00:00
CoffeeScript includes a (very) simple build system similar to
2010-07-29 04:51:35 +00:00
< a href = "http://www.gnu.org/software/make/" > Make< / a > and
2010-07-17 14:28:06 +00:00
< a href = "http://rake.rubyforge.org/" > Rake< / a > . Naturally,
2016-12-01 06:28:00 +00:00
it’ s called Cake, and is used for the tasks that build and test the CoffeeScript
2015-09-03 16:37:36 +00:00
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 > .
2010-02-21 23:37:52 +00:00
< / p >
< 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
2010-07-17 14:28:06 +00:00
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
2016-12-01 06:28:00 +00:00
be made available in the < code > options< / code > object. Here’ s a task that uses
the Node.js API to rebuild CoffeeScript’ s parser:
2010-07-17 14:28:06 +00:00
< / p >
2014-01-28 03:30:06 +00:00
< div class = 'code' > < pre > < code > fs = < span class = "built_in" > require< / span > < span class = "string" > 'fs'< / span >
2010-07-17 14:28:06 +00:00
2014-01-28 03:30:06 +00:00
option < span class = "string" > '-o'< / span > , < span class = "string" > '--output [DIR]'< / span > , < span class = "string" > 'directory for compiled code'< / span >
2010-07-17 14:28:06 +00:00
2014-01-28 03:30:06 +00:00
task < span class = "string" > 'build:parser'< / span > , < span class = "string" > 'rebuild the Jison parser'< / span > , < span class = "function" > < span class = "params" > (options)< / span > -> < / span >
< span class = "built_in" > require< / span > < span class = "string" > 'jison'< / span >
code = < span class = "built_in" > require< / span > (< span class = "string" > './lib/grammar'< / span > ).parser.generate()
dir = options.output < span class = "keyword" > or< / span > < span class = "string" > 'lib'< / span >
2016-09-22 20:19:43 +00:00
fs.writeFile < span class = "string" > "< span class = "subst" > #{dir}< / span > /parser.js"< / span > , code
< / code > < / pre > < pre > < code > < span class = "keyword" > var< / span > fs;
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
fs = < span class = "built_in" > require< / span > (< span class = "string" > 'fs'< / span > );
2011-09-21 03:42:19 +00:00
2014-01-28 03:30:06 +00:00
option(< span class = "string" > '-o'< / span > , < span class = "string" > '--output [DIR]'< / span > , < span class = "string" > 'directory for compiled code'< / span > );
2011-09-21 03:42:19 +00:00
2016-09-22 20:19:43 +00:00
task(< 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 > ) < / span > {
2014-01-28 03:30:06 +00:00
< span class = "keyword" > var< / span > code, dir;
< span class = "built_in" > require< / span > (< span class = "string" > 'jison'< / span > );
code = < span class = "built_in" > require< / span > (< span class = "string" > './lib/grammar'< / span > ).parser.generate();
dir = options.output || < span class = "string" > 'lib'< / span > ;
2015-01-29 17:20:46 +00:00
< span class = "keyword" > return< / span > fs.writeFile(dir + < span class = "string" > "/parser.js"< / span > , code);
2010-02-21 23:37:52 +00:00
});
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example50 = "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 > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example50);' > load< / div > < br class = 'clear' / > < / div >
2010-09-18 00:24:10 +00:00
< p >
2016-12-01 06:28:00 +00:00
If you need to invoke one task before another — for example, running
2015-09-03 16:37:36 +00:00
< 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
2011-08-14 20:24:26 +00:00
CoffeeScript functions to the command line, so
2016-12-01 06:28:00 +00:00
< a href = "v1/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.
2010-09-18 00:24:10 +00:00
< / p >
2009-12-31 18:01:10 +00:00
2013-03-04 19:19:08 +00:00
< h2 >
< span id = "source-maps" class = "bookmark" > < / span >
Source Maps
< / h2 >
< p >
2013-03-04 22:07:16 +00:00
CoffeeScript 1.6.1 and above include support for generating source maps,
2013-03-04 19:19:08 +00:00
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,
2015-09-03 16:37:36 +00:00
pass the < code > --map< / code > or < code > -m< / code > flag to the compiler.
2013-03-04 19:19:08 +00:00
< / p >
< p >
For a full introduction to source maps, how they work, and how to hook
them up in your browser, read the
< a href = "http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/" > HTML5 Tutorial< / a > .
< / p >
2010-02-25 05:26:59 +00:00
< h2 >
< span id = "scripts" class = "bookmark" > < / span >
"text/coffeescript" Script Tags
< / h2 >
< p >
2016-12-01 06:28:00 +00:00
While it’ s not recommended for serious use, CoffeeScripts may be included
2015-09-03 16:37:36 +00:00
directly within the browser using < code > < script type="text/coffeescript"> < / code >
2010-02-25 11:26:27 +00:00
tags. The source includes a compressed and minified version of the compiler
2016-12-01 06:28:00 +00:00
(< a href = "v1/browser-compiler/coffee-script.js" > Download current version here, 51k when gzipped< / a > )
2016-11-28 04:47:54 +00:00
as < code > v1/browser-compiler/coffee-script.js< / code > . Include this file on a page with
2010-02-25 11:26:27 +00:00
inline CoffeeScript tags, and it will compile and evaluate them in order.
2010-02-25 05:26:59 +00:00
< / p >
< p >
2016-12-01 06:28:00 +00:00
In fact, the little bit of glue script that runs “Try CoffeeScript” above,
2011-07-09 05:26:30 +00:00
as well as the jQuery for the menu, is implemented in just this way.
2010-02-25 05:26:59 +00:00
View source and look at the bottom of the page to see the example.
2015-09-03 16:37:36 +00:00
Including the script also gives you access to < code > CoffeeScript.compile()< / code >
2010-02-25 05:26:59 +00:00
so you can pop open Firebug and try compiling some strings.
< / p >
2010-02-25 23:20:15 +00:00
2010-02-25 05:26:59 +00:00
< p >
2016-12-01 06:28:00 +00:00
The usual caveats about CoffeeScript apply — your inline scripts will
2010-02-25 05:26:59 +00:00
run within a closure wrapper, so if you want to expose global variables or
2015-09-03 16:37:36 +00:00
functions, attach them to the < code > window< / code > object.
2010-02-25 05:26:59 +00:00
< / p >
2010-11-22 02:12:59 +00:00
2010-11-22 00:53:31 +00:00
< h2 >
2010-12-22 03:24:24 +00:00
< span id = "resources" class = "bookmark" > < / span >
2011-12-21 03:51:39 +00:00
Books
2011-08-05 03:45:38 +00:00
< / h2 >
2011-09-21 03:42:19 +00:00
2011-08-05 03:45:38 +00:00
< p >
2011-12-21 03:51:39 +00:00
There are a number of excellent resources to help you get
2011-08-05 03:45:38 +00:00
started with CoffeeScript, some of which are freely available online.
< / p >
2011-09-21 03:42:19 +00:00
2011-08-05 03:45:38 +00:00
< ul >
< li >
< a href = "http://arcturo.github.com/library/coffeescript/" > The Little Book on CoffeeScript< / a >
is a brief 5-chapter introduction to CoffeeScript, written with great
2011-09-21 03:42:19 +00:00
clarity and precision by
2011-08-05 03:45:38 +00:00
< a href = "http://alexmaccaw.co.uk/" > Alex MacCaw< / a > .
< / li >
< li >
< a href = "http://autotelicum.github.com/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
2013-02-25 08:12:22 +00:00
as well as the functional and object oriented programming styles. By
2011-08-05 03:45:38 +00:00
< a href = "https://github.com/autotelicum" > E. Hoigaard< / a > .
< / li >
< li >
< a href = "http://pragprog.com/book/tbcoffee/coffeescript" > CoffeeScript: Accelerated JavaScript Development< / a >
2016-12-01 06:28:00 +00:00
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
2011-08-05 03:45:38 +00:00
a fast-paced multiplayer word game, writing both the client-side and Node.js
portions in CoffeeScript.
< / li >
2013-01-28 10:06:07 +00:00
< li >
< a href = "http://www.packtpub.com/coffeescript-programming-with-jquery-rails-nodejs/book" > CoffeeScript Programming with jQuery, Rails, and Node.js< / a >
is a new book by Michael Erasmus that covers CoffeeScript with an eye
2016-09-22 20:19:43 +00:00
towards real-world usage both in the browser (jQuery) and on the
server-side (Rails, Node).
2013-01-28 10:06:07 +00:00
< / li >
2013-02-25 08:12:22 +00:00
< li >
2014-06-26 15:08:31 +00:00
< a href = "https://leanpub.com/coffeescript-ristretto/read" > CoffeeScript Ristretto< / a >
2016-12-01 06:28:00 +00:00
is a deep dive into CoffeeScript’ s semantics from simple functions up through
2013-02-25 08:12:22 +00:00
closures, higher-order functions, objects, classes, combinators, and decorators.
By < a href = "http://braythwayt.com/" > Reg Braithwaite< / a > .
< / li >
< 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 >
2013-09-22 17:56:33 +00:00
< li >
< a href = "http://www.packtpub.com/coffeescript-application-development/book" > CoffeeScript Application Development< / a >
2015-01-29 17:20:46 +00:00
from Packt, introduces CoffeeScript while
2013-09-22 17:56:33 +00:00
walking through the process of building a demonstration web application.
2015-04-15 16:03:22 +00:00
A < a href = "https://www.packtpub.com/web-development/coffeescript-application-development-cookbook" > CoffeeScript Application Development Coookbook< / a >
2016-12-01 06:28:00 +00:00
with over 90 “recipes” is also available.
2013-09-22 17:56:33 +00:00
< / li >
2014-01-28 01:56:29 +00:00
< li >
< a href = "http://www.manning.com/lee/" > CoffeeScript in Action< / a >
2015-01-29 17:20:46 +00:00
from Manning Publications, covers CoffeeScript syntax, composition techniques
2014-01-28 01:56:29 +00:00
and application development.
< / li >
2015-01-29 17:20:46 +00:00
< li >
< a href = "http://www.dpunkt.de/buecher/4021/coffeescript.html" > CoffeeScript: Die Alternative zu JavaScript< / a >
from dpunkt.verlag, is the first CoffeeScript book in Deutsch.
< / li >
2011-12-21 03:51:39 +00:00
< / ul >
2013-01-28 10:06:07 +00:00
2011-12-21 03:51:39 +00:00
< h2 >
Screencasts
< / h2 >
2013-01-28 10:06:07 +00:00
2011-12-21 03:51:39 +00:00
< ul >
< li >
< a href = "http://coffeescript.codeschool.com" > A Sip of CoffeeScript< / a > is a < a href = "http://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 >
2011-08-05 03:45:38 +00:00
< li >
< a href = "http://peepcode.com/products/coffeescript" > Meet CoffeeScript< / a >
is a 75-minute long screencast by < a href = "http://peepcode.com/" > PeepCode< / a > .
Highly memorable for its animations which demonstrate transforming CoffeeScript
into the equivalent JS.
< / li >
< li >
2016-12-01 06:28:00 +00:00
If you’ re looking for less of a time commitment, RailsCasts’
2011-08-05 03:45:38 +00:00
< 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 >
< / ul >
2011-09-21 03:42:19 +00:00
2011-08-05 03:45:38 +00:00
< h2 >
2010-11-22 00:53:31 +00:00
Examples
< / h2 >
2013-01-28 10:06:07 +00:00
2011-11-08 23:12:58 +00:00
< p >
2014-08-23 14:08:39 +00:00
The < a href = "https://github.com/trending?l=coffeescript&since=monthly" > best list of
2011-11-08 23:12:58 +00:00
open-source CoffeeScript examples< / a > can be found on GitHub. But just
2016-09-22 20:19:43 +00:00
to throw out a few more:
2011-11-08 23:12:58 +00:00
< / p >
2010-11-22 02:12:59 +00:00
2010-11-22 00:53:31 +00:00
< ul >
2011-11-08 23:12:58 +00:00
< li >
2016-12-02 05:42:17 +00:00
< b > GitHub< / b > ’ s < a href = "http://hubot.github.com/" > Hubot< / a > ,
2011-11-08 23:12:58 +00:00
a friendly IRC robot that can perform any number of useful and useless tasks.
< / li >
2011-04-21 03:08:21 +00:00
< li >
2016-12-01 06:28:00 +00:00
< b > sstephenson< / b > ’ s < a href = "http://pow.cx/" > Pow< / a > ,
2011-04-21 03:08:21 +00:00
a zero-configuration Rack server, with comprehensive annotated source.
< / li >
2010-11-22 00:53:31 +00:00
< li >
2016-12-01 06:28:00 +00:00
< b > technoweenie< / b > ’ s < a href = "https://github.com/technoweenie/coffee-resque" > Coffee-Resque< / a > ,
2010-11-22 00:53:31 +00:00
a port of < a href = "https://github.com/defunkt/resque" > Resque< / a > for Node.js.
< / li >
2011-04-21 03:08:21 +00:00
< li >
2016-12-01 06:28:00 +00:00
< b > assaf< / b > ’ s < a href = "http://zombie.labnotes.org/" > Zombie.js< / a > ,
2011-11-08 23:12:58 +00:00
a headless, full-stack, faux-browser testing library for Node.js.
2011-04-21 03:08:21 +00:00
< / li >
2010-11-22 00:53:31 +00:00
< li >
2016-12-01 06:28:00 +00:00
< b > stephank< / b > ’ s < a href = "https://github.com/stephank/orona" > Orona< / a > , a remake of
2010-11-22 00:53:31 +00:00
the Bolo tank game for modern browsers.
< / li >
2014-08-28 04:24:42 +00:00
< li >
2016-12-02 05:42:17 +00:00
< b > GitHub< / b > ’ s < a href = "https://atom.io/" > Atom< / a > ,
2014-08-28 04:24:42 +00:00
a hackable text editor built on web technologies.
< / li >
2016-12-02 05:42:17 +00:00
< li >
< b > Basecamp< / b > ’ s < a href = "https://trix-editor.org/" > Trix< / a > , a rich text editor for web apps.
< / li >
2010-11-22 00:53:31 +00:00
< / ul >
2010-02-25 05:26:59 +00:00
2010-02-21 19:30:21 +00:00
< h2 >
Resources
< / h2 >
2009-12-31 18:01:10 +00:00
2009-12-31 23:22:51 +00:00
< ul >
< li >
2014-05-08 19:07:42 +00:00
< a href = "http://github.com/jashkenas/coffeescript/" > Source Code< / a > < br / >
2015-09-03 16:37:36 +00:00
Use < code > bin/coffee< / code > to test your changes,< br / >
< code > bin/cake test< / code > to run the test suite,< br / >
< code > bin/cake build< / code > to rebuild the CoffeeScript compiler, and < br / >
2016-12-01 06:28:00 +00:00
< code > bin/cake build:parser< / code > to regenerate the Jison parser if you’ re
2010-03-08 02:57:08 +00:00
working on the grammar. < br / > < br / >
2016-12-01 06:28:00 +00:00
< 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 lib directory
2010-03-08 11:34:07 +00:00
(in case you broke something), build your altered compiler, use that to
2010-03-08 02:57:08 +00:00
rebuild itself (a good sanity test) and then run all of the tests. If
2016-12-01 06:28:00 +00:00
they pass, there’ s a good chance you’ ve made a successful change.
2009-12-31 23:22:51 +00:00
< / li >
2016-12-01 06:51:25 +00:00
< li >
< a href = "v1/test.html" > Browser Tests< / a > < br / >
Run CoffeeScript’ s test suite in your current browser.
< / li >
2009-12-31 23:22:51 +00:00
< li >
2014-05-08 19:07:42 +00:00
< a href = "http://github.com/jashkenas/coffeescript/issues" > CoffeeScript Issues< / a > < br / >
2011-04-21 03:08:21 +00:00
Bug reports, feature proposals, and ideas for changes to the language belong here.
2011-03-20 21:40:20 +00:00
< / li >
2010-07-11 14:40:44 +00:00
< li >
2011-04-21 03:08:21 +00:00
< a href = "https://groups.google.com/forum/#!forum/coffeescript" > CoffeeScript Google Group< / a > < br / >
2016-12-01 06:28:00 +00:00
If you’ d like to ask a question, the mailing list is a good place to get help.
2010-07-11 14:40:44 +00:00
< / li >
2010-04-14 03:41:18 +00:00
< li >
2014-05-08 19:07:42 +00:00
< a href = "http://github.com/jashkenas/coffeescript/wiki" > The CoffeeScript Wiki< / a > < br / >
2016-12-01 06:28:00 +00:00
If you’ ve ever learned a neat CoffeeScript tip or trick, or ran into a gotcha — share it on the wiki.
2011-09-21 03:42:19 +00:00
The wiki also serves as a directory of handy
2014-05-08 19:07:42 +00:00
< a href = "http://github.com/jashkenas/coffeescript/wiki/Text-editor-plugins" > text editor extensions< / a > ,
< a href = "http://github.com/jashkenas/coffeescript/wiki/Web-framework-plugins" > web framework plugins< / a > ,
and general < a href = "http://github.com/jashkenas/coffeescript/wiki/Build-tools" > CoffeeScript build tools< / a > .
2010-04-14 03:41:18 +00:00
< / li >
2011-01-30 22:10:50 +00:00
< li >
2014-05-08 19:07:42 +00:00
< a href = "http://github.com/jashkenas/coffeescript/wiki/FAQ" > The FAQ< / a > < br / >
2011-04-21 03:08:21 +00:00
Perhaps your CoffeeScript-related question has been asked before. Check the FAQ first.
2010-10-25 01:50:34 +00:00
< / li >
2013-03-18 05:06:33 +00:00
< li >
< a href = "http://js2coffee.org" > JS2Coffee< / a > < br / >
2016-12-01 06:28:00 +00:00
Is a very well done reverse JavaScript-to-CoffeeScript compiler. It’ s
2013-03-18 05:06:33 +00:00
not going to be perfect (infer what your JavaScript classes are, when
2016-12-01 06:28:00 +00:00
you need bound functions, and so on…) — but it’ s a great starting
2013-03-18 05:06:33 +00:00
point for converting simple scripts.
< / li >
2012-04-24 16:12:09 +00:00
< li >
2014-05-08 19:07:42 +00:00
< a href = "https://github.com/jashkenas/coffeescript/downloads" > High-Rez Logo< / a > < br / >
2012-04-24 16:12:09 +00:00
The CoffeeScript logo is available in Illustrator, EPS and PSD formats, for use
in presentations.
< / li >
2009-12-31 23:22:51 +00:00
< / ul >
2009-12-25 08:16:56 +00:00
2010-04-10 17:42:39 +00:00
< h2 >
< span id = "webchat" class = "bookmark" > < / span >
Web Chat (IRC)
< / h2 >
< p >
Quick help and advice can usually be found in the CoffeeScript IRC room.
2015-09-03 16:37:36 +00:00
Join < code > #coffeescript< / code > on < code > irc.freenode.net< / code > , or click the
2010-04-10 17:42:39 +00:00
button below to open a webchat session on this page.
< / p >
< p >
< button id = "open_webchat" > click to open #coffeescript< / button >
< / p >
2010-02-21 19:30:21 +00:00
< h2 >
2011-11-08 23:11:33 +00:00
< span id = "changelog" class = "bookmark" > < / span >
2010-02-21 19:30:21 +00:00
Change Log
< / h2 >
2013-01-28 10:06:07 +00:00
2016-12-07 05:15:04 +00:00
< p >
< div class = "anchor" id = "1.12.1" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/1.12.0...1.12.1" > 1.12.1< / a >
2016-12-07 22:27:51 +00:00
< span class = "timestamp" > — < time datetime = "2016-12-07" > December 7, 2016< / time > < / span >
2016-12-07 05:15:04 +00:00
< / b >
< ul >
< 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 >
< 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.
< / ul >
< / p >
2016-12-01 06:28:00 +00:00
< p >
< div class = "anchor" id = "1.12.0" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/1.11.1...1.12.0" > 1.12.0< / a >
2016-12-04 07:14:08 +00:00
< span class = "timestamp" > — < time datetime = "2016-12-04" > December 4, 2016< / time > < / span >
2016-12-01 06:28:00 +00:00
< / b >
< ul >
< 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 >
< 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
2016-12-05 01:29:46 +00:00
< code > for…of< / code > or that you transpile the output
2016-12-01 06:28:00 +00:00
JavaScript further to a version your target runtime(s) support.
< / li >
< 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 >
< li >
Within single-backtick embedded JavaScript, backticks can now be
escaped via < code > \`< / code > .
< / li >
< li >
The browser tests now run in the browser again, and are accessible
< a href = "v1/test.html" > here< / a > if you would like
to test your browser.
< / li >
< li >
CoffeeScript-only keywords in ES2015 < code > import< / code > s and
< code > export< / code > s are now ignored.
< / li >
< li >
The compiler now throws an error on trying to export an anonymous class.
< / li >
< li >
Bugfixes related to tokens and location data, for better source maps
and improved compatibility with downstream tools.
< / li >
< / ul >
< / p >
2016-10-01 18:58:53 +00:00
< p >
< div class = "anchor" id = "1.11.1" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/1.11.0...1.11.1" > 1.11.1< / a >
< span class = "timestamp" > — < time datetime = "2016-10-02" > October 2, 2016< / time > < / span >
< / b >
< ul >
< li >
Bugfix for shorthand object syntax after interpolated keys.
< / li >
< li >
Bugfix for indentation-stripping in < code > """< / code > strings.
< / li >
< li >
2016-12-01 06:28:00 +00:00
Bugfix for not being able to use the name “arguments” for a prototype
2016-10-01 18:58:53 +00:00
property of class.
< / li >
< li >
Correctly compile large hexadecimal numbers literals to
< code > 2e308< / code > (just like all other large number literals do).
< / li >
< / ul >
< / p >
2016-09-22 20:19:43 +00:00
< p >
< div class = "anchor" id = "1.11.0" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/1.10.0...1.11.0" > 1.11.0< / a >
< span class = "timestamp" > — < time datetime = "2016-09-24" > September 24, 2016< / time > < / span >
< / b >
< ul >
< li >
CoffeeScript now supports ES2015
< a href = "#modules" > < code > import< / code > and < code > export< / code > syntax< / a > .
< / li >
< li >
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.
< / li >
< li >
< p > A bunch of fixes for < code > yield< / code > :< / p >
< ul >
< li >
< code > yield return< / code > can no longer mistakenly be used as an expression.
< / li >
< li >
< 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 > , 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 >
< pre class = "no_bar" > doubles = ->
yield for i in [1..3]
i * 2
six = ->
yield
2 * 3< / pre >
< / li >
< li >
The JavaScript output is a bit nicer, with unnecessary parentheses
and spaces, double indentation and double semicolons around
< code > yield< / code > no longer present.
< / li >
< / ul >
< / li >
2016-09-26 13:09:53 +00:00
< li >
< code > & & =< / code > , < code > ||=< / code > , < code > and=< / code > and
< code > or=< / code > no longer accidentally allow a space before the
equals sign.
< / li >
2016-09-22 20:19:43 +00:00
< li >
Improved several error messages.
< / li >
< li >
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 > .
< / li >
< li >
2016-10-01 18:58:53 +00:00
Bugfix for renamed destructured parameters with defaults.
2016-09-22 20:19:43 +00:00
< code > ({a: b = 1}) -> < / code > no longer crashes the compiler.
< / li >
< li >
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.
< / li >
< li >
< p > Several minor bug fixes, including:< / p >
< ul >
< 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 >
< li >
Unassignable parameter destructuring no longer crashes the compiler.
< / li >
< li >
Source maps are now used correctly for errors thrown from .coffee.md files.
< / li >
< li >
< code > coffee -e 'throw null'< / code > no longer crashes.
< / li >
< li >
The REPL no longer crashes when using < code > .exit< / code > to exit it.
< / li >
< li >
Invalid JavaScript is no longer output when lots of
< code > for< / code > loops are used in the same scope.
< / li >
< li >
A unicode issue when using stdin with the CLI.
< / li >
< / ul >
< / ul >
< / p >
2015-09-03 16:37:36 +00:00
< p >
< div class = "anchor" id = "1.10.0" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/1.9.3...1.10.0" > 1.10.0< / a >
2015-09-03 20:08:34 +00:00
< span class = "timestamp" > — < time datetime = "2015-09-03" > September 3, 2015< / time > < / span >
2015-09-03 16:37:36 +00:00
< / b >
< ul >
< li >
2016-12-01 06:28:00 +00:00
CoffeeScript now supports ES2015-style destructuring defaults.
2015-09-03 16:37:36 +00:00
< / li >
< li >
< code > (offsetHeight: height) -> < / code > no longer compiles. That
2015-09-03 20:08:34 +00:00
syntax was accidental and partly broken. Use < code > ({offsetHeight: height}) -> < / code >
instead. Object destructuring always requires braces.
2015-09-03 16:37:36 +00:00
< / li >
< li >
< p > Several minor bug fixes, including:< / p >
< ul >
< li >
A bug where the REPL would sometimes report valid code as invalid,
based on what you had typed earlier.
< / li >
< li >
A problem with multiple JS contexts in the jest test framework.
< / li >
< li >
An error in io.js where strict mode is set on internal modules.
< / li >
< li >
A variable name clash for the caught error in < code > catch< / code >
blocks.
< / li >
< / ul >
< / li >
< / ul >
< / p >
2015-05-14 09:23:10 +00:00
< p >
< div class = "anchor" id = "1.9.3" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/1.9.2...1.9.3" > 1.9.3< / a >
2015-05-27 17:04:12 +00:00
< span class = "timestamp" > — < time datetime = "2015-05-27" > May 27, 2015< / time > < / span >
2015-05-14 09:23:10 +00:00
< / b >
< ul >
< li >
Bugfix for interpolation in the first key of an object literal in an
implicit call.
< / li >
< li >
Fixed broken error messages in the REPL, as well as a few minor bugs
with the REPL.
< / li >
< li >
Fixed source mappings for tokens at the beginning of lines when
2015-09-03 16:37:36 +00:00
compiling with the < code > --bare< / code > option. This has the nice side
2015-05-14 09:23:10 +00:00
effect of generating smaller source maps.
< / li >
< li >
Slight formatting improvement of compiled block comments.
< / li >
< li >
2015-09-03 16:37:36 +00:00
Better error messages for < code > on< / code > , < code > off< / code > , < code > yes< / code > and
< code > no< / code > .
2015-05-14 09:23:10 +00:00
< / li >
< / ul >
< / p >
2015-04-15 15:26:30 +00:00
< p >
< div class = "anchor" id = "1.9.2" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/1.9.1...1.9.2" > 1.9.2< / a >
< span class = "timestamp" > — < time datetime = "2015-04-15" > April 15, 2015< / time > < / span >
< / b >
< ul >
< li >
2015-04-15 15:49:14 +00:00
Fixed a < b > watch< / b > mode error introduced in 1.9.1 when compiling
multiple files with the same filename.
2015-04-15 15:26:30 +00:00
< / li >
< li >
2015-09-03 16:37:36 +00:00
Bugfix for < code > yield< / code > around expressions containing
< code > this< / code > .
2015-04-15 15:26:30 +00:00
< / li >
< li >
2015-09-03 16:37:36 +00:00
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 > .
2015-04-15 15:26:30 +00:00
< / li >
< li >
2015-09-03 16:37:36 +00:00
In < code > < script type="text/coffeescript"> < / code > tags, to avoid
2016-09-22 20:19:43 +00:00
possible duplicate browser requests for .coffee files,
2015-09-03 16:37:36 +00:00
you can now use the < code > data-src< / code > attribute instead of < code > src< / code > .
2015-04-15 15:26:30 +00:00
< / li >
< li >
2015-04-15 15:49:14 +00:00
Minor bug fixes for IE8, strict ES5 regular expressions and Browserify.
2015-04-15 15:26:30 +00:00
< / li >
< / ul >
< / p >
2015-02-18 20:43:33 +00:00
< p >
< div class = "anchor" id = "1.9.1" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/1.9.0...1.9.1" > 1.9.1< / a >
< span class = "timestamp" > — < time datetime = "2015-02-18" > February 18, 2015< / time > < / span >
< / b >
< ul >
< li >
2015-04-15 15:26:30 +00:00
Interpolation now works in object literal keys (again). You can use this to
2015-02-18 20:43:33 +00:00
dynamically name properties.
< / li >
< li >
2015-02-18 20:48:45 +00:00
Internal compiler variable names no longer start with underscores. This makes
2015-02-18 20:43:33 +00:00
the generated JavaScript a bit prettier, and also fixes an issue with
2016-12-01 06:28:00 +00:00
the completely broken and ungodly way that AngularJS “parses” function
2015-02-18 20:43:33 +00:00
arguments.
< / li >
< li >
2015-09-03 16:37:36 +00:00
Fixed a few < code > yield< / code > -related edge cases with < code > yield return< / code >
and < code > yield throw< / code > .
2015-02-18 20:43:33 +00:00
< / li >
< li >
Minor bug fixes and various improvements to compiler error messages.
< / li >
< / ul >
< / p >
2015-01-29 17:20:46 +00:00
< p >
< div class = "anchor" id = "1.9.0" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/1.8.0...1.9.0" > 1.9.0< / a >
< span class = "timestamp" > — < time datetime = "2015-01-29" > January 29, 2015< / time > < / span >
< / b >
< ul >
< li >
2016-12-01 06:28:00 +00:00
CoffeeScript now supports ES2015 generators. A generator is simply a function
2015-09-03 16:37:36 +00:00
that < code > yield< / code > s.
2015-01-29 17:20:46 +00:00
< / li >
< li >
2015-04-15 15:26:30 +00:00
More robust parsing and improved error messages for strings and regexes —
2015-01-29 19:58:49 +00:00
especially with respect to interpolation.
2015-01-29 17:20:46 +00:00
< / li >
< li >
Changed strategy for the generation of internal compiler variable names.
2015-09-03 16:37:36 +00:00
Note that this means that < code > @example< / code > function parameters are no longer
available as naked < code > example< / code > variables within the function body.
2015-01-29 17:20:46 +00:00
< / li >
< li >
2015-01-29 19:58:49 +00:00
Fixed REPL compatibility with latest versions of Node and Io.js.
< / li >
< li >
Various minor bug fixes.
2015-01-29 17:20:46 +00:00
< / li >
< / ul >
< / p >
2014-08-23 14:09:51 +00:00
< p >
< div class = "anchor" id = "1.8.0" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/1.7.1...1.8.0" > 1.8.0< / a >
< span class = "timestamp" > — < time datetime = "2014-08-26" > August 26, 2014< / time > < / span >
< / b >
< ul >
< li >
2015-09-03 16:37:36 +00:00
The < code > --join< / code > option of the CLI is now deprecated.
2014-08-23 14:09:51 +00:00
< / li >
< li >
2015-09-03 16:37:36 +00:00
Source maps now use < code > .js.map< / code > as file extension, instead of just < code > .map< / code > .
2014-08-23 14:09:51 +00:00
< / li >
< li >
The CLI now exits with the exit code 1 when it fails to write a file to disk.
< / li >
< li >
The compiler no longer crashes on unterminated, single-quoted strings.
< / li >
< li >
Fixed location data for string interpolations, which made source maps out of sync.
< / li >
< li >
The error marker in error messages is now correctly positioned if the code is indented with tabs.
< / li >
< li >
Fixed a slight formatting error in CoffeeScript’ s source map-patched stack traces.
< / li >
< li >
2015-09-03 16:37:36 +00:00
The < code > %%< / code > operator now coerces its right operand only once.
2014-08-23 14:09:51 +00:00
< / li >
< li >
It is now possible to require CoffeeScript files from Cakefiles without having to register the compiler first.
< / li >
< li >
2015-09-03 16:37:36 +00:00
The CoffeeScript REPL is now exported and can be required using < code > require 'coffee-script/repl'< / code > .
2014-08-23 14:09:51 +00:00
< / li >
< li >
Fixes for the REPL in Node 0.11.
< / li >
< / ul >
< / p >
2014-01-28 01:56:29 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "1.7.1" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/1.7.0...1.7.1" > 1.7.1< / a >
< span class = "timestamp" > — < time datetime = "2014-01-29" > January 29, 2014< / time > < / span >
< / b >
2014-01-30 05:07:27 +00:00
< ul >
< li >
2015-09-03 16:37:36 +00:00
Fixed a typo that broke node module lookup when running a script directly with the < code > coffee< / code > binary.
2014-01-30 05:07:27 +00:00
< / li >
< / ul >
< / p >
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "1.7.0" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/1.6.3...1.7.0" > 1.7.0< / a >
< span class = "timestamp" > — < time datetime = "2014-01-28" > January 28, 2014< / time > < / span >
< / b >
2014-01-28 01:56:29 +00:00
< ul >
< li >
2016-12-01 06:28:00 +00:00
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 < b > coffee-script/register< / b > .
2014-01-28 01:56:29 +00:00
< / li >
< li >
2015-09-03 16:37:36 +00:00
Improved error messages, source maps and stack traces. Source maps now use the updated < code > //#< / code > syntax.
2014-01-28 01:56:29 +00:00
< / li >
< li >
2015-09-03 16:37:36 +00:00
Leading < code > .< / code > now closes all open calls, allowing for simpler chaining syntax.
2014-01-28 01:56:29 +00:00
< / li >
2014-01-28 03:30:06 +00:00
< / ul >
< div class = 'code' > < pre > < code > $ < span class = "string" > 'body'< / span >
2016-09-22 20:19:43 +00:00
.click (e) ->
2014-01-28 03:30:06 +00:00
$ < span class = "string" > '.box'< / span >
.fadeIn < span class = "string" > 'fast'< / span >
.addClass < span class = "string" > '.active'< / span >
.css < span class = "string" > 'background'< / span > , < span class = "string" > 'white'< / span >
2016-09-22 20:19:43 +00:00
< / code > < / pre > < pre > < code > $(< span class = "string" > 'body'< / span > ).click(< span class = "function" > < span class = "keyword" > function< / span > (< span class = "params" > e< / span > ) < / span > {
2014-01-28 03:30:06 +00:00
< span class = "keyword" > return< / span > $(< span class = "string" > '.box'< / span > ).fadeIn(< span class = "string" > 'fast'< / span > ).addClass(< span class = "string" > '.active'< / span > );
}).css(< span class = "string" > 'background'< / span > , < span class = "string" > 'white'< / span > );
2016-12-01 06:28:00 +00:00
< / code > < / pre > < script > window . example51 = "$ 'body'\n.click (e) ->\n $ '.box'\n .fadeIn 'fast'\n .addClass '.active'\n.css 'background', 'white'\n" < / script > < div class = 'minibutton load' onclick = 'javascript: loadConsole(example51);' > load< / div > < br class = 'clear' / > < / div >
2014-01-28 03:30:06 +00:00
< ul >
2014-01-28 01:56:29 +00:00
< li >
2015-09-03 16:37:36 +00:00
Added < code > **< / code > , < code > //< / code > and < code > %%< / code > operators and < code > ...< / code > expansion in parameter lists and destructuring expressions.
2014-01-28 01:56:29 +00:00
< / li >
< 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 >
< li >
Closing brackets can now be indented and therefore no longer cause unexpected error.
< / li >
< li >
2016-12-01 06:28:00 +00:00
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.
2014-01-28 01:56:29 +00:00
< / li >
< li >
Formatting of compiled block comments has been improved.
< / li >
< li >
2015-09-03 16:37:36 +00:00
No more < code > -p< / code > folders on Windows.
2014-01-28 01:56:29 +00:00
< / li >
< li >
2015-09-03 16:37:36 +00:00
The < code > options< / code > object passed to CoffeeScript is no longer mutated.
2014-01-28 01:56:29 +00:00
< / li >
< / ul >
< / p >
2013-06-02 05:37:45 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "1.6.3" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/1.6.2...1.6.3" > 1.6.3< / a >
< span class = "timestamp" > — < time datetime = "2013-06-02" > June 2, 2013< / time > < / span >
< / b >
2013-06-02 05:37:45 +00:00
< ul >
< li >
The CoffeeScript REPL now remembers your history between sessions.
Just like a proper REPL should.
< / li >
< li >
2015-09-03 16:37:36 +00:00
You can now use < code > require< / code > in Node to load < code > .coffee.md< / code >
2013-06-02 05:37:45 +00:00
Literate CoffeeScript files. In the browser,
2015-09-03 16:37:36 +00:00
< code > text/literate-coffeescript< / code > script tags.
2013-06-02 05:37:45 +00:00
< / li >
< li >
2015-09-03 16:37:36 +00:00
The old < code > coffee --lint< / code > command has been removed. It was useful
2013-06-02 05:37:45 +00:00
while originally working on the compiler, but has been surpassed by
2015-09-03 16:37:36 +00:00
JSHint. You may now use < code > -l< / code > to pass literate files in over
2013-06-02 05:37:45 +00:00
< b > stdio< / b > .
< / li >
< li >
2015-09-03 16:37:36 +00:00
Bugfixes for Windows path separators, < code > catch< / code > without naming
2013-06-02 05:37:45 +00:00
the error, and executable-class-bodies-with-
prototypal-property-attachment.
< / li >
< / ul >
< / p >
2013-03-18 05:06:33 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "1.6.2" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/1.6.1...1.6.2" > 1.6.2< / a >
< span class = "timestamp" > — < time datetime = "2013-03-18" > March 18, 2013< / time > < / span >
< / b >
2013-03-18 05:06:33 +00:00
< ul >
< li >
Source maps have been used to provide automatic line-mapping when
2015-09-03 16:37:36 +00:00
running CoffeeScript directly via the < code > coffee< / code > command, and
2013-03-18 05:06:33 +00:00
for automatic line-mapping when running CoffeeScript directly in the
browser. Also, to provide better error messages for semantic errors
2016-12-01 06:28:00 +00:00
thrown by the compiler —
2013-03-18 05:06:33 +00:00
< a href = "http://cl.ly/NdOA" > with colors, even< / a > .
< / li >
< li >
Improved support for mixed literate/vanilla-style CoffeeScript projects,
and generating source maps for both at the same time.
< / li >
< li >
Fixes for < b > 1.6.x< / b > regressions with overriding inherited bound
functions, and for Windows file path management.
< / li >
< li >
2015-09-03 16:37:36 +00:00
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+)
2013-03-18 05:06:33 +00:00
< / li >
< / ul >
< / p >
2013-03-04 19:19:08 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "1.6.1" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/1.5.0...1.6.1" > 1.6.1< / a >
< span class = "timestamp" > — < time datetime = "2013-03-05" > March 5, 2013< / time > < / span >
< / b >
2013-03-04 19:19:08 +00:00
< ul >
< li >
First release of < a href = "#source-maps" > source maps< / a > . Pass the
2015-09-03 16:37:36 +00:00
< code > --map< / code > flag to the compiler, and off you go. Direct all your
2013-03-04 19:19:08 +00:00
thanks over to < a href = "http://github.com/jwalton" > Jason Walton< / a > .
< / li >
< li >
Fixed a 1.5.0 regression with multiple implicit calls against an
indented implicit object. Combinations of implicit function calls
2016-12-01 06:28:00 +00:00
and implicit objects should generally be parsed better now —
but it still isn’ t good < i > style< / i > to nest them too heavily.
2013-03-04 19:19:08 +00:00
< / li >
< li >
2015-09-03 16:37:36 +00:00
< code > .coffee.md< / code > is now also supported as a Literate CoffeeScript
2013-03-04 19:19:08 +00:00
file extension, for existing tooling.
2015-09-03 16:37:36 +00:00
< code > .litcoffee< / code > remains the canonical one.
2013-03-04 19:19:08 +00:00
< / li >
< li >
Several minor fixes surrounding member properties, bound methods and
2015-09-03 16:37:36 +00:00
< code > super< / code > in class declarations.
2013-03-04 19:19:08 +00:00
< / li >
< / ul >
< / p >
2013-02-25 08:12:22 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "1.5.0" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/1.4.0...1.5.0" > 1.5.0< / a >
< span class = "timestamp" > — < time datetime = "2013-02-25" > February 25, 2013< / time > < / span >
< / b >
2013-02-25 08:12:22 +00:00
< ul >
< li >
First release of < a href = "#literate" > Literate CoffeeScript< / a > .
< / li >
< li >
The CoffeeScript REPL is now based on the Node.js REPL, and should work
better and more familiarly.
< / li >
< li >
Returning explicit values from constructors is now forbidden. If you want
to return an arbitrary value, use a function, not a constructor.
< / li >
< li >
You can now loop over an array backwards, without having to manually
2015-09-03 16:37:36 +00:00
deal with the indexes: < code > for item in list by -1< / code >
2013-02-25 08:12:22 +00:00
< / li >
< li >
Source locations are now preserved in the CoffeeScript AST, although
source maps are not yet being emitted.
< / li >
< / ul >
< / p >
2012-10-23 20:45:31 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "1.4.0" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/1.3.3...1.4.0" > 1.4.0< / a >
< span class = "timestamp" > — < time datetime = "2012-10-23" > October 23, 2012< / time > < / span >
< / b >
2012-10-23 20:45:31 +00:00
< ul >
< li >
2016-12-01 06:28:00 +00:00
The CoffeeScript compiler now strips Microsoft’ s UTF-8 BOM if it
2012-10-23 20:45:31 +00:00
exists, allowing you to compile BOM-borked source files.
< / li >
< li >
2015-09-03 16:37:36 +00:00
Fix Node/compiler deprecation warnings by removing < code > registerExtension< / code > ,
and moving from < code > path.exists< / code > to < code > fs.exists< / code > .
2012-10-23 20:45:31 +00:00
< / li >
< li >
2013-01-28 10:06:07 +00:00
Small tweaks to splat compilation, backticks, slicing, and the
2012-10-23 20:45:31 +00:00
error for duplicate keys in object literals.
< / li >
< / ul >
< / p >
2013-01-28 10:06:07 +00:00
2012-05-14 18:45:20 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "1.3.3" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/1.3.1...1.3.3" > 1.3.3< / a >
< span class = "timestamp" > — < time datetime = "2012-05-15" > May 15, 2012< / time > < / span >
< / b >
2012-05-14 18:45:20 +00:00
< ul >
< li >
2016-12-01 06:28:00 +00:00
Due to the new semantics of JavaScript’ s strict mode, CoffeeScript no
2012-05-14 18:45:20 +00:00
longer guarantees that constructor functions have names in all runtimes.
2014-05-08 19:07:42 +00:00
See < a href = "https://github.com/jashkenas/coffeescript/issues/2052" > #2052< / a >
2012-05-14 18:45:20 +00:00
for discussion.
< / li >
< li >
2016-12-01 06:28:00 +00:00
Inside of a nested function inside of an instance method, it’ s now possible
2015-09-03 16:37:36 +00:00
to call < code > super< / code > more reliably (walks recursively up).
2012-05-14 18:45:20 +00:00
< / li >
< li >
Named loop variables no longer have different scoping heuristics than
other local variables. (Reverts #643)
< / li >
< li >
Fix for splats nested within the LHS of destructuring assignment.
< / li >
< li >
Corrections to our compile time strict mode forbidding of octal literals.
< / li >
< / ul >
< / p >
2013-01-28 10:06:07 +00:00
2012-04-10 18:57:45 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "1.3.1" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/1.2.0...1.3.1" > 1.3.1< / a >
< span class = "timestamp" > — < time datetime = "2012-04-10" > April 10, 2012< / time > < / span >
< / b >
2012-04-10 18:57:45 +00:00
< ul >
< li >
2016-12-01 06:28:00 +00:00
CoffeeScript now enforces all of JavaScript’ s < b > Strict Mode< / b > early syntax
2013-01-28 10:06:07 +00:00
errors at compile time. This includes old-style octal literals,
duplicate property names in object literals, duplicate parameters in
2012-04-10 18:57:45 +00:00
a function definition, deleting naked variables, setting the value of
2015-09-03 16:37:36 +00:00
< code > eval< / code > or < code > arguments< / code > , and more.
2013-01-28 10:06:07 +00:00
See a full discussion at
2014-05-08 19:07:42 +00:00
< a href = "https://github.com/jashkenas/coffeescript/issues/1547" > #1547< / a > .
2012-04-10 18:57:45 +00:00
< / li >
< li >
The REPL now has a handy new multi-line mode for entering large
2016-12-01 06:28:00 +00:00
blocks of code. It’ s useful when copy-and-pasting examples into the
2015-09-03 16:37:36 +00:00
REPL. Enter multi-line mode with < code > Ctrl-V< / code > . You may also now
2012-04-10 18:57:45 +00:00
pipe input directly into the REPL.
< / li >
< li >
2015-09-03 16:37:36 +00:00
CoffeeScript now prints a < code > Generated by CoffeeScript VERSION< / code >
2012-04-10 18:57:45 +00:00
header at the top of each compiled file.
< / li >
< li >
2013-01-28 10:06:07 +00:00
Conditional assignment of previously undefined variables
2015-09-03 16:37:36 +00:00
< code > a or= b< / code > is now considered a syntax error.
2012-04-10 18:57:45 +00:00
< / li >
< li >
2015-09-03 16:37:36 +00:00
A tweak to the semantics of < code > do< / code > , which can now be used to
2016-12-01 06:28:00 +00:00
more easily simulate a namespace: < code > do (x = 1, y = 2) -> …< / code >
2012-04-10 18:57:45 +00:00
< / li >
< li >
Loop indices are now mutable within a loop iteration, and immutable
between them.
< / li >
< li >
2013-01-28 10:06:07 +00:00
Both endpoints of a slice are now allowed to be omitted for consistency,
2012-04-10 18:57:45 +00:00
effectively creating a shallow copy of the list.
< / li >
< li >
2015-09-03 16:37:36 +00:00
Additional tweaks and improvements to < code > coffee --watch< / code > under
2016-12-01 06:28:00 +00:00
Node’ s “new” file watching API. Watch will now beep by default
2012-04-10 18:57:45 +00:00
if you introduce a syntax error into a watched script. We also now
ignore hidden directories by default when watching recursively.
< / li >
< / ul >
< / p >
2013-01-28 10:06:07 +00:00
2011-12-18 19:26:23 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "1.2.0" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/1.1.3...1.2.0" > 1.2.0< / a >
< span class = "timestamp" > — < time datetime = "2011-12-18" > December 18, 2011< / time > < / span >
< / b >
2011-12-18 19:26:23 +00:00
< ul >
< li >
2015-09-03 16:37:36 +00:00
Multiple improvements to < code > coffee --watch< / code > and < code > --join< / code > .
2011-12-18 19:26:23 +00:00
You may now use both together, as well as add and remove
2016-12-01 06:28:00 +00:00
files and directories within a < code > --watch< / code > ’ d folder.
2011-12-18 19:26:23 +00:00
< / li >
< li >
2015-09-03 16:37:36 +00:00
The < code > throw< / code > statement can now be used as part of an expression.
2011-12-18 19:26:23 +00:00
< / li >
< li >
Block comments at the top of the file will now appear outside of the
safety closure wrapper.
< / li >
< 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 bound functions < i > within< / i > bound class functions to have the incorrect
2015-09-03 16:37:36 +00:00
< code > this< / code > .
2011-12-18 19:26:23 +00:00
< / li >
< / ul >
< / p >
2013-01-28 10:06:07 +00:00
2011-11-08 23:01:45 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "1.1.3" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/1.1.2...1.1.3" > 1.1.3< / a >
< span class = "timestamp" > — < time datetime = "2011-11-08" > November 8, 2011< / time > < / span >
< / b >
2011-11-08 23:01:45 +00:00
< ul >
< li >
2016-12-01 06:28:00 +00:00
Ahh, whitespace. CoffeeScript’ s compiled JS now tries to space things
2011-11-08 23:01:45 +00:00
out and keep it readable, as you can see in the examples on this page.
< / li >
< li >
2015-09-03 16:37:36 +00:00
You can now call < code > super< / code > in class level methods in class bodies,
2011-11-08 23:12:58 +00:00
and bound class methods now preserve their correct context.
< / li >
< li >
2015-09-03 16:37:36 +00:00
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 > .
2011-11-08 23:01:45 +00:00
< / li >
< li >
2013-01-28 10:06:07 +00:00
The CoffeeScript module has been nested under a subdirectory to make
2015-09-03 16:37:36 +00:00
it easier to < code > require< / code > individual components separately, without
2011-11-08 23:12:58 +00:00
having to use < b > npm< / b > . For example, after adding the CoffeeScript
2015-09-03 16:37:36 +00:00
folder to your path: < code > require('coffee-script/lexer')< / code >
2011-11-08 23:01:45 +00:00
< / li >
< li >
2016-12-01 06:28:00 +00:00
There’ s a new “link” feature in Try CoffeeScript on this webpage. Use
2011-11-08 23:12:58 +00:00
it to get a shareable permalink for your example script.
2011-11-08 23:01:45 +00:00
< / li >
< li >
2015-09-03 16:37:36 +00:00
The < code > coffee --watch< / code > feature now only works on Node.js 0.6.0
2011-11-08 23:12:58 +00:00
and higher, but now also works properly on Windows.
2011-11-08 23:01:45 +00:00
< / li >
< li >
2013-01-28 10:06:07 +00:00
Lots of small bug fixes from
2011-11-08 23:12:58 +00:00
< b > < a href = "https://github.com/michaelficarra" > @michaelficarra< / a > < / b > ,
< b > < a href = "https://github.com/geraldalewis" > @geraldalewis< / a > < / b > ,
< b > < a href = "https://github.com/satyr" > @satyr< / a > < / b > , and
< b > < a href = "https://github.com/trevorburnham" > @trevorburnham< / a > < / b > .
2011-11-08 23:01:45 +00:00
< / li >
< / ul >
< / p >
2011-09-21 03:42:19 +00:00
2011-08-05 03:17:23 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "1.1.2" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/1.1.1...1.1.2" > 1.1.2< / a >
< span class = "timestamp" > — < time datetime = "2011-08-04" > August 4, 2011< / time > < / span >
< / b >
2015-09-03 16:37:36 +00:00
Fixes for block comment formatting, < code > ?=< / code > compilation, implicit calls
2011-08-05 03:17:23 +00:00
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,
2011-09-21 03:42:19 +00:00
bound class methods and super with reserved names, a REPL overhaul,
2011-08-05 03:17:23 +00:00
consecutive compiled semicolons, block comments in implicitly called objects,
and a Chrome bug.
< / p >
2011-09-21 03:42:19 +00:00
2011-05-10 13:27:19 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "1.1.1" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/1.1.0...1.1.1" > 1.1.1< / a >
< span class = "timestamp" > — < time datetime = "2011-05-10" > May 10, 2011< / time > < / span >
< / b >
2011-09-21 03:42:19 +00:00
Bugfix release for classes with external constructor functions, see
2011-05-10 13:27:19 +00:00
issue #1182.
< / p >
2011-09-21 03:42:19 +00:00
2011-05-01 16:08:15 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "1.1.0" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/1.0.1...1.1.0" > 1.1.0< / a >
< span class = "timestamp" > — < time datetime = "2011-05-01" > May 1, 2011< / time > < / span >
< / b >
2015-09-03 16:37:36 +00:00
When running via the < code > coffee< / code > executable, < code > process.argv< / code > and
friends now report < code > coffee< / code > instead of < code > node< / code > .
2011-05-01 16:08:15 +00:00
Better compatibility with < b > Node.js 0.4.x< / b > module lookup changes.
2016-12-01 06:28:00 +00:00
The output in the REPL is now colorized, like Node’ s is.
2015-09-03 16:37:36 +00:00
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.
2016-12-01 06:28:00 +00:00
All < code > text/coffeescript< / code > tags should now execute in the order they’ re included.
2011-05-01 16:08:15 +00:00
Fixed an issue with extended subclasses using external constructor functions.
2015-09-03 16:37:36 +00:00
Fixed an edge-case infinite loop in < code > addImplicitParentheses< / code > .
2011-05-01 16:08:15 +00:00
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 >
2011-04-21 03:08:21 +00:00
2011-02-01 03:39:12 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "1.0.1" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/1.0.0...1.0.1" > 1.0.1< / a >
< span class = "timestamp" > — < time datetime = "2011-01-31" > January 31, 2011< / time > < / span >
< / b >
2011-02-01 03:39:12 +00:00
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
2015-09-03 16:37:36 +00:00
< code > return< / code > and < code > return undefined< / code > are now optimized away.
2016-12-01 06:28:00 +00:00
Stopped requiring the core Node.js < code > util< / code > module for
2011-02-01 03:39:12 +00:00
back-compatibility with Node.js 0.2.5. Fixed a case where a
2015-09-03 16:37:36 +00:00
conditional < code > return< / code > would cause fallthrough in a < code > switch< / code >
2011-02-01 03:39:12 +00:00
statement. Optimized empty objects in destructuring assignment.
< / p >
2010-12-12 00:44:11 +00:00
2010-12-24 19:02:10 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "1.0.0" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/0.9.6...1.0.0" > 1.0.0< / a >
< span class = "timestamp" > — < time datetime = "2010-12-24" > December 24, 2010< / time > < / span >
< / b >
2010-12-24 19:02:10 +00:00
CoffeeScript loops no longer try to preserve block scope when functions
are being generated within the loop body. Instead, you can use the
2015-09-03 16:37:36 +00:00
< 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.
2010-12-24 19:02:10 +00:00
Better behavior around the use of pure statements within expressions.
2015-09-03 16:37:36 +00:00
Fixed inclusive slicing through < code > -1< / code > , for all browsers, and splicing
2010-12-24 19:02:10 +00:00
with arbitrary expressions as endpoints.
< / p >
2010-12-07 05:21:34 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "0.9.6" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/0.9.5...0.9.6" > 0.9.6< / a >
< span class = "timestamp" > — < time datetime = "2010-12-06" > December 6, 2010< / time > < / span >
< / b >
2010-12-07 05:21:34 +00:00
The REPL now properly formats stacktraces, and stays alive through
2015-09-03 16:37:36 +00:00
asynchronous exceptions. Using < code > --watch< / code > now prints timestamps as
2010-12-07 05:21:34 +00:00
files are compiled. Fixed some accidentally-leaking variables within
2010-12-12 00:44:11 +00:00
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
2010-12-07 05:21:34 +00:00
splatted functions. Bugfix for inversion of chained comparisons.
Chained class instantiation now works properly with splats.
< / p >
2010-09-27 19:21:45 +00:00
2010-11-22 02:12:59 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "0.9.5" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/0.9.4...0.9.5" > 0.9.5< / a >
< span class = "timestamp" > — < time datetime = "2010-11-21" > November 21, 2010< / time > < / span >
< / b >
2010-11-22 02:12:59 +00:00
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,
2016-12-01 06:28:00 +00:00
many contributed from < b > satyr< / b > ’ s < a href = "http://github.com/satyr/coco" > Coco< / a >
2010-11-22 02:12:59 +00:00
dialect of CoffeeScript. Heregexes (extended regexes) were added. Functions
can now have default arguments. Class bodies are now executable code.
2015-09-03 16:37:36 +00:00
Improved syntax errors for invalid CoffeeScript. < code > undefined< / code > now
works like < code > null< / code > , and cannot be assigned a new value.
2010-12-01 19:17:10 +00:00
There was a precedence change with respect to single-line comprehensions:
2015-09-03 16:37:36 +00:00
< code > result = i for i in list< / code > < br / > used to parse as < code > result = (i for i in list)< / code >
2016-12-01 06:28:00 +00:00
by default … it now parses as < br / > < code > (result = i) for i in list< / code > .
2010-11-22 02:12:59 +00:00
< / p >
2010-09-22 03:58:05 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "0.9.4" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/0.9.3...0.9.4" > 0.9.4< / a >
< span class = "timestamp" > — < time datetime = "2010-09-21" > September 21, 2010< / time > < / span >
< / b >
2010-09-22 03:58:05 +00:00
CoffeeScript now uses appropriately-named temporary variables, and recycles
2015-09-03 16:37:36 +00:00
their references after use. Added < code > require.extensions< / code > support for
2010-09-22 03:58:05 +00:00
< b > Node.js 0.3< / b > . Loading CoffeeScript in the browser now adds just a
2015-09-03 16:37:36 +00:00
single < code > CoffeeScript< / code > object to global scope.
2010-09-22 03:58:05 +00:00
Fixes for implicit object and block comment edge cases.
< / p >
2010-09-27 19:21:45 +00:00
2010-09-16 04:32:57 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "0.9.3" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/0.9.2...0.9.3" > 0.9.3< / a >
< span class = "timestamp" > — < time datetime = "2010-09-16" > September 16, 2010< / time > < / span >
< / b >
2015-09-03 16:37:36 +00:00
CoffeeScript < code > switch< / code > statements now compile into JS < code > switch< / code >
2016-12-01 06:28:00 +00:00
statements — they previously compiled into < code > if/else< / code > chains
2010-09-16 04:32:57 +00:00
for JavaScript 1.3 compatibility.
Soaking a function invocation is now supported. Users of the RubyMine
2015-09-03 16:37:36 +00:00
editor should now be able to use < code > --watch< / code > mode.
2010-09-16 04:32:57 +00:00
< / p >
2010-08-24 02:08:33 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "0.9.2" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/0.9.1...0.9.2" > 0.9.2< / a >
< span class = "timestamp" > — < time datetime = "2010-08-23" > August 23, 2010< / time > < / span >
< / b >
2015-09-03 16:37:36 +00:00
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 > .
2010-08-24 02:08:33 +00:00
Fixed important bugs with nested significant and non-significant indentation (Issue #637).
2015-09-03 16:37:36 +00:00
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.
2010-08-24 02:08:33 +00:00
Sped up Jison grammar compilation time by flattening rules for operations.
Block comments can now be used with JavaScript-minifier-friendly syntax.
2016-12-01 06:28:00 +00:00
Added JavaScript’ s compound assignment bitwise operators. Bugfixes to
2010-08-24 02:08:33 +00:00
implicit object literals with leading number and string keys, as the subject
of implicit calls, and as part of compound assignment.
< / p >
2010-08-11 04:40:15 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "0.9.1" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/0.9.0...0.9.1" > 0.9.1< / a >
< span class = "timestamp" > — < time datetime = "2010-08-11" > August 11, 2010< / time > < / span >
< / b >
2010-08-11 04:40:15 +00:00
Bugfix release for < b > 0.9.1< / b > . Greatly improves the handling of mixed
2010-08-24 02:08:33 +00:00
implicit objects, implicit function calls, and implicit indentation.
2016-12-01 06:28:00 +00:00
String and regex interpolation is now strictly < code > #{ … }< / code > (Ruby style).
2015-09-03 16:37:36 +00:00
The compiler now takes a < code > --require< / code > flag, which specifies scripts
2010-08-11 04:40:15 +00:00
to run before compilation.
< / p >
2010-07-17 14:28:06 +00:00
2010-08-03 05:13:31 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "0.9.0" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/0.7.2...0.9.0" > 0.9.0< / a >
< span class = "timestamp" > — < time datetime = "2010-08-04" > August 4, 2010< / time > < / span >
< / b >
2010-08-03 05:13:31 +00:00
The CoffeeScript < b > 0.9< / b > series is considered to be a release candidate
2016-12-01 06:28:00 +00:00
for < b > 1.0< / b > ; let’ s give her a shakedown cruise. < b > 0.9.0< / b > introduces a massive
2015-09-03 16:37:36 +00:00
backwards-incompatible change: Assignment now uses < code > =< / code > , and object
literals use < code > :< / code > , as in JavaScript. This allows us to have implicit
2010-08-03 05:13:31 +00:00
object literals, and YAML-style object definitions. Half assignments are
2015-09-03 16:37:36 +00:00
removed, in favor of < code > +=< / code > , < code > or=< / code > , and friends.
Interpolation now uses a hash mark < code > #< / code > instead of the dollar sign
2016-12-01 06:28:00 +00:00
< code > $< / code > — because dollar signs may be part of a valid JS identifier.
2010-08-03 05:13:31 +00:00
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 comprehension was added:
2015-09-03 16:37:36 +00:00
< code > for all key, value of object< / code > . Mentioning the < code > super< / code > keyword
2010-08-03 05:13:31 +00:00
with no arguments now forwards all arguments passed to the function,
2015-09-03 16:37:36 +00:00
as in Ruby. If you extend class < code > B< / code > from parent class < code > A< / code > , if
2016-12-01 06:28:00 +00:00
< code > A< / code > has an < code > extended< / code > method defined, it will be called, passing in < code > B< / code > —
2010-08-03 05:13:31 +00:00
this enables static inheritance, among other things. Cleaner output for
2015-09-03 16:37:36 +00:00
functions bound with the fat arrow. < code > @variables< / code > can now be used
2010-08-03 05:13:31 +00:00
in parameter lists, with the parameter being automatically set as a property
2016-12-01 06:28:00 +00:00
on the object — useful in constructors and setter functions.
2010-08-03 05:13:31 +00:00
Constructor functions can now take splats.
< / p >
2010-08-24 02:08:33 +00:00
2010-07-14 03:39:58 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "0.7.2" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/0.7.1...0.7.2" > 0.7.2< / a >
< span class = "timestamp" > — < time datetime = "2010-07-12" > July 12, 2010< / time > < / span >
< / b >
2015-09-03 16:37:36 +00:00
Quick bugfix (right after 0.7.1) for a problem that prevented < code > coffee< / code >
2010-07-14 03:39:58 +00:00
command-line options from being parsed in some circumstances.
< / p >
2010-07-17 14:28:06 +00:00
2010-07-11 14:40:44 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "0.7.1" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/0.7.0...0.7.1" > 0.7.1< / a >
< span class = "timestamp" > — < time datetime = "2010-07-11" > July 11, 2010< / time > < / span >
< / b >
2010-07-11 14:40:44 +00:00
Block-style comments are now passed through and printed as JavaScript block
2016-12-01 06:28:00 +00:00
comments – making them useful for licenses and copyright headers. Better
2010-07-11 14:40:44 +00:00
support for running coffee scripts standalone via hashbangs.
Improved syntax errors for tokens that are not in the grammar.
< / p >
2010-07-17 14:28:06 +00:00
2010-06-28 05:00:53 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "0.7.0" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/0.6.2...0.7.0" > 0.7.0< / a >
< span class = "timestamp" > — < time datetime = "2010-06-28" > June 28, 2010< / time > < / span >
< / b >
2010-06-28 05:00:53 +00:00
Official CoffeeScript variable style is now camelCase, as in JavaScript.
Reserved words are now allowed as object keys, and will be quoted for you.
2015-09-03 16:37:36 +00:00
Range comprehensions now generate cleaner code, but you have to specify < code > by -1< / code >
2016-12-01 06:28:00 +00:00
if you’ d like to iterate downward. Reporting of syntax errors is greatly
2015-09-03 16:37:36 +00:00
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 > .
2010-06-28 05:00:53 +00:00
You can now use bound function in class definitions (bound to the instance).
2015-09-03 16:37:36 +00:00
For consistency, < code > a in b< / code > is now an array presence check, and < code > a of b< / code >
2010-06-28 05:00:53 +00:00
is an object-key check. Comments are no longer passed through to the generated
JavaScript.
< / p >
2010-05-15 05:18:05 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "0.6.2" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/0.6.1...0.6.2" > 0.6.2< / a >
< span class = "timestamp" > — < time datetime = "2010-05-15" > May 15, 2010< / time > < / span >
< / b >
2015-09-03 16:37:36 +00:00
The < code > coffee< / code > command will now preserve directory structure when
2010-05-15 05:18:05 +00:00
compiling a directory full of scripts. Fixed two omissions that were preventing
the CoffeeScript compiler from running live within Internet Explorer.
2016-12-01 06:28:00 +00:00
There’ s now a syntax for block comments, similar in spirit to CoffeeScript’ s heredocs.
2010-05-15 05:18:05 +00:00
ECMA Harmony DRY-style pattern matching is now supported, where the name
2015-09-03 16:37:36 +00:00
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
2010-05-15 05:18:05 +00:00
switch object clauses. Compatible
with Node.js < b > v0.1.95< / b > .
< / p >
2010-04-13 01:20:00 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "0.6.1" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/0.6.0...0.6.1" > 0.6.1< / a >
< span class = "timestamp" > — < time datetime = "2010-04-12" > April 12, 2010< / time > < / span >
< / b >
2010-04-13 01:20:00 +00:00
Upgraded CoffeeScript for compatibility with the new Node.js < b > v0.1.90< / b >
series.
< / p >
2010-04-10 17:42:39 +00:00
2010-04-04 00:43:50 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "0.6.0" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/0.5.6...0.6.0" > 0.6.0< / a >
< span class = "timestamp" > — < time datetime = "2010-04-03" > April 3, 2010< / time > < / span >
< / b >
2010-04-10 17:42:39 +00:00
Trailing commas are now allowed, a-la Python. Static
properties may be assigned directly within class definitions,
2015-09-03 16:37:36 +00:00
using < code > @property< / code > notation.
2010-04-04 00:43:50 +00:00
< / p >
2010-03-08 11:34:07 +00:00
2010-03-23 04:18:50 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "0.5.6" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/0.5.5...0.5.6" > 0.5.6< / a >
< span class = "timestamp" > — < time datetime = "2010-03-23" > March 23, 2010< / time > < / span >
< / b >
2010-03-23 04:18:50 +00:00
Interpolation can now be used within regular expressions and heredocs, as well as
2015-09-03 16:37:36 +00:00
strings. Added the < code > < -< / code > bind operator.
Allowing assignment to half-expressions instead of special < code > ||=< / code > -style
2010-03-23 04:18:50 +00:00
operators. The arguments object is no longer automatically converted into
2015-09-03 16:37:36 +00:00
an array. After requiring < code > coffee-script< / code > , Node.js can now directly
load < code > .coffee< / code > files, thanks to < b > registerExtension< / b > . Multiple
2010-03-23 04:18:50 +00:00
splats can now be used in function calls, arrays, and pattern matching.
< / p >
2010-03-08 11:34:07 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "0.5.5" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/0.5.4...0.5.5" > 0.5.5< / a >
< span class = "timestamp" > — < time datetime = "2010-03-08" > March 8, 2010< / time > < / span >
< / b >
2010-03-08 11:34:07 +00:00
String interpolation, contributed by
< a href = "http://github.com/StanAngeloff" > Stan Angeloff< / a > .
2015-09-03 16:37:36 +00:00
Since < code > --run< / code > has been the default since < b > 0.5.3< / b > , updating
< code > --stdio< / code > and < code > --eval< / code > to run by default, pass < code > --compile< / code >
2016-12-01 06:28:00 +00:00
as well if you’ d like to print the result.
2010-03-08 11:34:07 +00:00
< / p >
2010-03-04 04:01:53 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "0.5.4" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/0.5.3...0.5.4" > 0.5.4< / a >
< span class = "timestamp" > — < time datetime = "2010-03-03" > March 3, 2010< / time > < / span >
< / b >
2015-09-03 16:37:36 +00:00
Bugfix that corrects the Node.js global constants < code > __filename< / code > and
< code > __dirname< / code > . Tweaks for more flexible parsing of nested function
2010-03-04 04:01:53 +00:00
literals and improperly-indented comments. Updates for the latest Node.js API.
< / p >
2010-03-08 11:34:07 +00:00
2010-02-28 01:21:46 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "0.5.3" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/0.5.2...0.5.3" > 0.5.3< / a >
< span class = "timestamp" > — < time datetime = "2010-02-27" > February 27, 2010< / time > < / span >
< / b >
2010-03-08 11:34:07 +00:00
CoffeeScript now has a syntax for defining classes. Many of the core
2010-02-28 01:21:46 +00:00
components (Nodes, Lexer, Rewriter, Scope, Optparse) are using them.
2015-09-03 16:37:36 +00:00
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
2010-02-28 01:21:46 +00:00
RegExp literals and chained divisions.
< / p >
2010-02-25 23:20:15 +00:00
2010-02-25 05:26:59 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "0.5.2" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/0.5.1...0.5.2" > 0.5.2< / a >
< span class = "timestamp" > — < time datetime = "2010-02-25" > February 25, 2010< / time > < / span >
< / b >
2010-02-25 05:26:59 +00:00
Added a compressed version of the compiler for inclusion in web pages as
2016-12-01 06:28:00 +00:00
< br / > < code > v1/browser-compiler/coffee-script.js< / code > . It’ ll automatically run any script tags
2015-09-03 16:37:36 +00:00
with type < code > text/coffeescript< / code > for you. Added a < code > --stdio< / code > option
to the < code > coffee< / code > command, for piped-in compiles.
2010-02-25 05:26:59 +00:00
< / p >
2010-02-25 23:20:15 +00:00
2010-02-24 23:27:10 +00:00
2010-02-24 05:54:07 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "0.5.1" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/0.5.0...0.5.1" > 0.5.1< / a >
< span class = "timestamp" > — < time datetime = "2010-02-24" > February 24, 2010< / time > < / span >
< / b >
2010-02-24 05:54:07 +00:00
Improvements to null soaking with the existential operator, including
2015-09-03 16:37:36 +00:00
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
2010-02-24 05:54:07 +00:00
comprehensions.
< / p >
2010-02-22 04:29:31 +00:00
2010-02-21 23:37:52 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "0.5.0" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/0.3.2...0.5.0" > 0.5.0< / a >
< span class = "timestamp" > — < time datetime = "2010-02-21" > February 21, 2010< / time > < / span >
< / b >
2010-02-22 00:19:58 +00:00
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.
2010-02-21 23:37:52 +00:00
< / p >
2010-02-13 20:25:04 +00:00
2010-02-08 15:58:49 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "0.3.2" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/0.3.0...0.3.2" > 0.3.2< / a >
< span class = "timestamp" > — < time datetime = "2010-02-08" > February 8, 2010< / time > < / span >
< / b >
2015-09-03 16:37:36 +00:00
< code > @property< / code > is now a shorthand for < code > this.property< / code > .< br / >
2010-02-08 15:58:49 +00:00
Switched the default JavaScript engine from Narwhal to Node.js. Pass
2016-12-01 06:28:00 +00:00
the < code > --narwhal< / code > flag if you’ d like to continue using it.
2010-02-08 15:58:49 +00:00
< / p >
2009-12-25 08:16:56 +00:00
2009-12-24 17:54:12 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "0.3.0" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/0.2.6...0.3.0" > 0.3.0< / a >
< span class = "timestamp" > — < time datetime = "2010-01-26" > January 26, 2010< / time > < / span >
< / b >
2010-01-27 04:23:59 +00:00
CoffeeScript 0.3 includes major syntax changes:
< br / >
The function symbol was changed to
2015-09-03 16:37:36 +00:00
< code > ->< / code > , and the bound function symbol is now < code > =>< / code > .
2010-01-27 04:23:59 +00:00
< br / >
Parameter lists in function definitions must now be wrapped in parentheses.
< br / >
2015-09-03 16:37:36 +00:00
Added property soaking, with the < code > ?.< / code > operator.
2010-01-27 04:23:59 +00:00
< br / >
Made parentheses optional, when invoking functions with arguments.
< br / >
Removed the obsolete block literal syntax.
2009-12-24 17:54:12 +00:00
< / p >
2009-12-25 08:16:56 +00:00
2010-01-17 23:12:59 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "0.2.6" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/0.2.5...0.2.6" > 0.2.6< / a >
< span class = "timestamp" > — < time datetime = "2010-01-17" > January 17, 2010< / time > < / span >
< / b >
2010-01-17 23:12:59 +00:00
Added Python-style chained comparisons, the conditional existence
2015-09-03 16:37:36 +00:00
operator < code > ?=< / code > , and some examples from < i > Beautiful Code< / i > .
2010-01-17 23:12:59 +00:00
Bugfixes relating to statement-to-expression conversion, arguments-to-array
conversion, and the TextMate syntax highlighter.
< / p >
2010-01-14 04:24:45 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "0.2.5" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/0.2.4...0.2.5" > 0.2.5< / a >
< span class = "timestamp" > — < time datetime = "2010-01-13" > January 13, 2010< / time > < / span >
< / b >
2016-12-01 06:28:00 +00:00
The conditions in switch statements can now take multiple values at once —
2015-09-03 16:37:36 +00:00
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
2010-01-14 04:24:45 +00:00
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 >
2010-01-12 05:09:23 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "0.2.4" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/0.2.3...0.2.4" > 0.2.4< / a >
< span class = "timestamp" > — < time datetime = "2010-01-12" > January 12, 2010< / time > < / span >
< / b >
2010-01-12 05:09:23 +00:00
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 >
2010-01-12 03:55:01 +00:00
2010-01-11 05:01:16 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "0.2.3" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/0.2.2...0.2.3" > 0.2.3< / a >
< span class = "timestamp" > — < time datetime = "2010-01-11" > January 11, 2010< / time > < / span >
< / b >
2015-09-03 16:37:36 +00:00
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 > .
2010-01-11 05:01:16 +00:00
< / p >
2009-12-31 18:01:10 +00:00
2010-01-10 19:45:44 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "0.2.2" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/0.2.1...0.2.2" > 0.2.2< / a >
< span class = "timestamp" > — < time datetime = "2010-01-10" > January 10, 2010< / time > < / span >
< / b >
2015-09-03 16:37:36 +00:00
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
2010-01-10 21:16:59 +00:00
compile time.
< br / >
2015-09-03 16:37:36 +00:00
Added < code > ::< / code > as a shorthand for saying < code > .prototype.< / code >
2010-01-10 21:16:59 +00:00
< br / >
2016-12-01 06:28:00 +00:00
The “splat” symbol has been changed from a prefix asterisk < code > *< / code > , to
2015-09-03 16:37:36 +00:00
a postfix ellipsis < code > ...< / code >
2010-01-10 21:16:59 +00:00
< br / >
2016-12-01 06:28:00 +00:00
Added JavaScript’ s < code > in< / code > operator,
2015-09-03 16:37:36 +00:00
empty < code > return< / code > statements, and empty < code > while< / code > loops.
2010-01-10 21:16:59 +00:00
< br / >
2010-01-10 19:45:44 +00:00
Constructor functions that start with capital letters now include a
safety check to make sure that the new instance of the object is returned.
2010-01-10 21:16:59 +00:00
< br / >
2015-09-03 16:37:36 +00:00
The < code > extends< / code > keyword now functions identically to < code > goog.inherits< / code >
2016-12-01 06:28:00 +00:00
in Google’ s Closure Library.
2010-01-10 19:45:44 +00:00
< / p >
2010-01-05 14:29:10 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "0.2.1" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/0.2.0...0.2.1" > 0.2.1< / a >
< span class = "timestamp" > — < time datetime = "2010-01-05" > January 5, 2010< / time > < / span >
< / b >
2010-01-05 14:29:10 +00:00
Arguments objects are now converted into real arrays when referenced.
< / p >
2010-01-05 05:19:22 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "0.2.0" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/0.1.6...0.2.0" > 0.2.0< / a >
< span class = "timestamp" > — < time datetime = "2010-01-05" > January 5, 2010< / time > < / span >
< / b >
2010-01-05 05:19:22 +00:00
Major release. Significant whitespace. Better statement-to-expression
conversion. Splats. Splice literals. Object comprehensions. Blocks.
2010-01-17 23:12:59 +00:00
The existential operator. Many thanks to all the folks who posted issues,
2010-01-05 14:29:10 +00:00
with special thanks to
2016-12-01 06:28:00 +00:00
< a href = "http://github.com/liamoc" > Liam O’ Connor-Davis< / a > for whitespace
2010-01-05 05:19:22 +00:00
and expression help.
< / p >
2009-12-27 20:49:11 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "0.1.6" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/0.1.5...0.1.6" > 0.1.6< / a >
< span class = "timestamp" > — < time datetime = "2009-12-27" > December 27, 2009< / time > < / span >
< / b >
2015-09-03 16:37:36 +00:00
Bugfix for running < code > coffee --interactive< / code > and < code > --run< / code >
2009-12-31 18:01:10 +00:00
from outside of the CoffeeScript directory. Bugfix for nested
function/if-statements.
2009-12-27 20:49:11 +00:00
< / p >
2009-12-31 18:01:10 +00:00
2009-12-27 05:25:37 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "0.1.5" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/0.1.4...0.1.5" > 0.1.5< / a >
< span class = "timestamp" > — < time datetime = "2009-12-26" > December 26, 2009< / time > < / span >
< / b >
2009-12-27 05:25:37 +00:00
Array slice literals and array comprehensions can now both take Ruby-style
2009-12-31 18:01:10 +00:00
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
2015-09-03 16:37:36 +00:00
expressions. You can use < code > \< / code > to escape newlines.
The < code > coffee-script< / code > command is now called < code > coffee< / code > .
2009-12-27 05:25:37 +00:00
< / p >
2009-12-26 00:35:57 +00:00
2009-12-25 22:43:24 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "0.1.4" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/0.1.3...0.1.4" > 0.1.4< / a >
< span class = "timestamp" > — < time datetime = "2009-12-25" > December 25, 2009< / time > < / span >
< / b >
2015-09-03 16:37:36 +00:00
The official CoffeeScript extension is now < code > .coffee< / code > instead of
< code > .cs< / code > , which properly belongs to
2009-12-25 22:43:24 +00:00
< a href = "http://en.wikipedia.org/wiki/C_Sharp_(programming_language)" > C#< / a > .
2015-09-03 16:37:36 +00:00
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.
2009-12-25 22:43:24 +00:00
< / p >
2009-12-25 22:18:05 +00:00
2009-12-25 08:16:56 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "0.1.3" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/0.1.2...0.1.3" > 0.1.3< / a >
< span class = "timestamp" > — < time datetime = "2009-12-25" > December 25, 2009< / time > < / span >
< / b >
2015-09-03 16:37:36 +00:00
The < code > coffee< / code > command now includes < code > --interactive< / code > ,
which launches an interactive CoffeeScript session, and < code > --run< / code > ,
2009-12-25 08:16:56 +00:00
which directly compiles and executes a script. Both options depend on a
working installation of Narwhal.
2015-09-03 16:37:36 +00:00
The < code > aint< / code > keyword has been replaced by < code > isnt< / code > , which goes
together a little smoother with < code > is< / code > .
2009-12-25 08:16:56 +00:00
Quoted strings are now allowed as identifiers within object literals: eg.
2015-09-03 16:37:36 +00:00
< code > {"5+5": 10}< / code > .
All assignment operators now use a colon: < code > +:< / code > , < code > -:< / code > ,
< code > *:< / code > , etc.
2009-12-25 08:16:56 +00:00
< / p >
2009-12-24 20:02:28 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "0.1.2" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/0.1.1...0.1.2" > 0.1.2< / a >
< span class = "timestamp" > — < time datetime = "2009-12-24" > December 24, 2009< / time > < / span >
< / b >
2015-09-03 16:37:36 +00:00
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.
2009-12-25 08:16:56 +00:00
Added experimental < a href = "http://narwhaljs.org/" > Narwhal< / a >
support (as a Tusk package), contributed by
2016-12-04 07:49:44 +00:00
< a href = "http://blog.tlrobinson.net/" > Tom Robinson< / a > , including
2009-12-25 01:05:55 +00:00
< b > bin/cs< / b > as a CoffeeScript REPL and interpreter.
2015-09-03 16:37:36 +00:00
New < code > --no-wrap< / code > option to suppress the safety function
2009-12-25 01:05:55 +00:00
wrapper.
< / p >
2009-12-25 08:16:56 +00:00
2009-12-25 01:05:55 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "0.1.1" > < / div >
< b class = "header" >
< a href = "https://github.com/jashkenas/coffeescript/compare/0.1.0...0.1.1" > 0.1.1< / a >
< span class = "timestamp" > — < time datetime = "2009-12-24" > December 24, 2009< / time > < / span >
< / b >
2015-09-03 16:37:36 +00:00
Added < code > instanceof< / code > and < code > typeof< / code > as operators.
2009-12-24 20:02:28 +00:00
< / p >
2009-12-25 08:16:56 +00:00
2009-12-24 08:12:07 +00:00
< p >
2014-08-23 14:08:39 +00:00
< div class = "anchor" id = "0.1.0" > < / div >
< b class = "header" >
0.1.0
< span class = "timestamp" > — < time datetime = "2009-12-24" > December 24, 2009< / time > < / span >
< / b >
2009-12-24 08:12:07 +00:00
Initial CoffeeScript release.
< / p >
2009-12-21 16:41:45 +00:00
2009-12-20 03:55:58 +00:00
< / div >
2010-02-25 01:41:56 +00:00
< script type = "text/coffeescript" >
2011-09-21 03:42:19 +00:00
sourceFragment = "try:"
2010-02-25 01:41:56 +00:00
2010-02-26 01:39:34 +00:00
# Set up the compilation function, to run when you stop typing.
2010-12-13 02:41:04 +00:00
compileSource = ->
2010-08-03 04:06:34 +00:00
source = $('#repl_source').val()
2014-08-23 14:08:39 +00:00
results = $('#repl_results')
2010-12-13 02:41:04 +00:00
window.compiledJS = ''
2010-02-25 01:41:56 +00:00
try
2010-12-13 02:41:04 +00:00
window.compiledJS = CoffeeScript.compile source, bare: on
2014-08-23 14:08:39 +00:00
el = results[0]
2010-12-24 19:02:10 +00:00
if el.innerText
el.innerText = window.compiledJS
else
2014-08-23 14:08:39 +00:00
results.text(window.compiledJS)
results.removeClass 'error'
$('.minibutton.run').removeClass 'error'
2013-03-18 05:06:33 +00:00
catch {location, message}
if location?
message = "Error on line #{location.first_line + 1}: #{message}"
2014-08-23 14:08:39 +00:00
results.text(message).addClass 'error'
$('.minibutton.run').addClass 'error'
2010-02-26 01:39:34 +00:00
2011-09-18 21:56:09 +00:00
# Update permalink
2011-09-21 03:42:19 +00:00
$('#repl_permalink').attr 'href', "##{sourceFragment}#{encodeURIComponent source}"
2011-09-18 21:56:09 +00:00
2010-02-26 01:39:34 +00:00
# Listen for keypresses and recompile.
2010-12-13 02:41:04 +00:00
$('#repl_source').keyup -> compileSource()
2010-02-25 01:41:56 +00:00
2016-11-21 01:05:19 +00:00
# Use tab key to insert tabs
$('#repl_source').keydown (e) ->
if e.keyCode is 9
e.preventDefault()
textbox = e.target
# Insert tab character at caret or in selection
textbox.value = textbox.value[0...textbox.selectionStart] + "\t" + textbox.value[textbox.selectionEnd...]
# Put caret in correct position
textbox.selectionEnd = ++textbox.selectionStart
2010-02-26 01:39:34 +00:00
# Eval the compiled js.
2011-02-10 16:28:02 +00:00
evalJS = ->
2010-02-25 01:41:56 +00:00
try
2010-12-13 02:41:04 +00:00
eval window.compiledJS
2010-02-25 01:41:56 +00:00
catch error then alert error
2010-12-13 02:41:04 +00:00
# Load the console with a string of CoffeeScript.
window.loadConsole = (coffee) ->
$('#repl_source').val coffee
compileSource()
2010-12-13 02:53:17 +00:00
$('.navigation.try').addClass('active')
false
2010-02-25 01:41:56 +00:00
2010-02-26 01:39:34 +00:00
# Helper to hide the menus.
2010-12-13 02:41:04 +00:00
closeMenus = ->
2010-12-13 02:53:17 +00:00
$('.navigation.active').removeClass 'active'
2010-02-25 01:41:56 +00:00
2012-04-25 18:37:05 +00:00
$('.minibutton.run').click -> evalJS()
2011-02-10 16:28:02 +00:00
2010-02-26 01:39:34 +00:00
# Bind navigation buttons to open the menus.
$('.navigation').click (e) ->
2010-02-25 01:41:56 +00:00
return if e.target.tagName.toLowerCase() is 'a'
2010-12-13 02:41:04 +00:00
return false if $(e.target).closest('.repl_wrapper').length
2010-12-13 02:53:17 +00:00
if $(this).hasClass('active')
2010-12-13 02:41:04 +00:00
closeMenus()
else
closeMenus()
2010-12-13 02:53:17 +00:00
$(this).addClass 'active'
2010-02-25 01:41:56 +00:00
false
2011-02-10 16:28:02 +00:00
# Dismiss console if Escape pressed or click falls outside console
# Trigger Run button on Ctrl-Enter
$(document.body)
.keydown (e) ->
closeMenus() if e.which == 27
evalJS() if e.which == 13 and (e.metaKey or e.ctrlKey) and $('.minibutton.run:visible').length
.click (e) ->
return false if $(e.target).hasClass('minibutton')
closeMenus()
2010-02-27 07:21:26 +00:00
2010-04-10 17:42:39 +00:00
$('#open_webchat').click ->
$(this).replaceWith $('< iframe src = "http://webchat.freenode.net/?channels=coffeescript" width = "625" height = "400" > < / iframe > ')
2011-09-18 21:56:09 +00:00
$("#repl_permalink").click (e) ->
window.location = $(this).attr("href")
2011-09-19 01:43:10 +00:00
false
2011-09-18 21:56:09 +00:00
# If source code is included in location.hash, display it.
hash = decodeURIComponent location.hash.replace(/^#/, '')
2011-09-21 03:42:19 +00:00
if hash.indexOf(sourceFragment) == 0
src = hash.substr sourceFragment.length
2011-09-18 21:56:09 +00:00
loadConsole src
2010-12-13 02:41:04 +00:00
compileSource()
2010-02-26 01:39:34 +00:00
2010-02-25 01:41:56 +00:00
< / script >
2010-02-22 04:29:31 +00:00
2016-11-16 04:54:30 +00:00
< script src = "https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js" > < / script >
2016-11-28 04:47:54 +00:00
< script src = "v1/browser-compiler/coffee-script.js" > < / script >
2010-02-13 21:23:03 +00:00
2009-12-20 03:55:58 +00:00
< / body >
< / html >