Skip to content

Commit 77ead1c

Browse files
committed
Merge pull request coffeescript-cookbook#2 from sreid/master
added syntax/embedding_javascript
2 parents 49c4aa3 + ba10734 commit 77ead1c

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
layout: recipe
3+
title: Embedding JavaScript
4+
chapter: Syntax
5+
---
6+
7+
h2. Problem
8+
9+
You want to include some found/pre-written JavaScript code inline with your CoffeeScript
10+
11+
h2. Solution
12+
13+
Wrap the JavaScript with backticks:
14+
15+
{% highlight coffeescript %}
16+
`function greet(name) {
17+
alert("Hello "+name);
18+
}`
19+
20+
# Back to CoffeeScript
21+
greet "Coffee"
22+
{% endhighlight %}
23+
24+
h2. Discussion
25+
26+
This is a simple way to integrate small snippets of JavaScript code into your CoffeeScript without converting it over to use CoffeeScript syntax. As shown in the "CoffeeScript Language Reference":http://jashkenas.github.com/coffee-script/#embedded you can mix to the two languages to a certain extent:
27+
28+
{% highlight coffeescript %}
29+
hello = `function (name) {
30+
alert("Hello "+name)
31+
}`
32+
hello "Coffee"
33+
34+
{% endhighlight %}
35+
Here the "hello" variable is still in CoffeeScript, but is assigned a function written in JavaScript.

0 commit comments

Comments
 (0)