Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'gh-pages'

  • Loading branch information...
commit 01cf70514d6e2dcbe0bf1a6488797815a956dae3 2 parents b2795a7 + dc24515
@maccman maccman authored
View
23 coffeescript/04_idioms.html
@@ -141,6 +141,29 @@
Math.min [14, 35, -7, 46, 98]... # -7
</code></pre>
+<h2>Multiple arguments</h2>
+
+<p>In the <code>Math.max</code> example above, we're using <code>...</code> to de-structure the array and passing it as multiple arguments to <code>max</code>. Behind the scenes, CoffeeScript is converting the function call to use <code>apply()</code>, ensuring the array is passed as multiple arguments to <code>max</code>. We can use this feature in other ways to, such as proxying function calls:</p>
+
+<pre><code>Log =
+ log: -&gt;
+ return if typeof console is "undefined"
+ console.log(arguments...)
+</code></pre>
+
+<p>Or you can alter the arguments before they're passed onwards:</p>
+
+<pre><code>Log =
+ logPrefix: "(App)"
+
+ log: (args...) -&gt;
+ return if typeof console is "undefined"
+ if @logPrefix then args.unshift(@logPrefix)
+ console.log(args...)
+</code></pre>
+
+<p>Bear in mind though, that CoffeeScript will automatically set the function invocation context to the object the function is being invoked on. In the example above, that would be <code>console</code>. If you want to set the context specifically, then you'll need to call <code>apply()</code> manually.</p>
+
<h2>And/or</h2>
<p>CoffeeScript style guides indicates that <code>or</code> is preferred over <code>||</code>, and <code>and</code> is preferred over <code>&amp;&amp;</code>. I can see why, as the former is somewhat more readable. Nevertheless, the two styles have identical results.</p>
View
2  coffeescript/05_applications.html
@@ -93,7 +93,7 @@
var package = stitch.createPackage({
// Specify the paths you want Stitch to automatically bundle up
- paths: [__dirname + '/lib', __dirname + '/app'],
+ paths: [__dirname + '/app'],
// Specify your base libraries
dependencies: [
View
21 coffeescript/chapters/04_idioms.md
@@ -106,6 +106,27 @@ This technique is not specific to CoffeeScript, but I thought it useful to demon
Math.max [14, 35, -7, 46, 98]... # 98
Math.min [14, 35, -7, 46, 98]... # -7
+
+##Multiple arguments
+
+In the `Math.max` example above, we're using `...` to de-structure the array and passing it as multiple arguments to `max`. Behind the scenes, CoffeeScript is converting the function call to use `apply()`, ensuring the array is passed as multiple arguments to `max`. We can use this feature in other ways to, such as proxying function calls:
+
+ Log =
+ log: ->
+ return if typeof console is "undefined"
+ console.log(arguments...)
+
+Or you can alter the arguments before they're passed onwards:
+
+ Log =
+ logPrefix: "(App)"
+
+ log: (args...) ->
+ return if typeof console is "undefined"
+ if @logPrefix then args.unshift(@logPrefix)
+ console.log(args...)
+
+Bear in mind though, that CoffeeScript will automatically set the function invocation context to the object the function is being invoked on. In the example above, that would be `console`. If you want to set the context specifically, then you'll need to call `apply()` manually.
##And/or
View
2  coffeescript/chapters/05_applications.md
@@ -65,7 +65,7 @@ Now to actually boot up the Stitch server. Let's create a file called `server.js
var package = stitch.createPackage({
// Specify the paths you want Stitch to automatically bundle up
- paths: [__dirname + '/lib', __dirname + '/app'],
+ paths: [__dirname + '/app'],
// Specify your base libraries
dependencies: [
Please sign in to comment.
Something went wrong with that request. Please try again.