Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions chapters/syntax/embedding_javascript.textile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
layout: recipe
title: Embedding JavaScript
chapter: Syntax
---

h2. Problem

You want to include some found/pre-written JavaScript code inline with your CoffeeScript

h2. Solution

Wrap the JavaScript with backticks:

{% highlight coffeescript %}
`function greet(name) {
alert("Hello "+name);
}`

# Back to CoffeeScript
greet "Coffee"
{% endhighlight %}

h2. Discussion

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:

{% highlight coffeescript %}
hello = `function (name) {
alert("Hello "+name)
}`
hello "Coffee"

{% endhighlight %}
Here the "hello" variable is still in CoffeeScript, but is assigned a function written in JavaScript.