CoffeeScript sandbox

by bh88

HTML

<script src="http://codemirror.net/lib/codemirror.js"></script>
<link rel="stylesheet" href="http://codemirror.net/lib/codemirror.css">
<script src="http://codemirror.net/mode/javascript/javascript.js"></script>
<script type="text/javascript">$.get('http://fiddle.jshell.net/bh88/SKMpV/show/',function(){$('#code').val(CoffeeScript.compile($('script[type*=coffeescript]').html()));var editor=CodeMirror.fromTextArea(document.getElementById('code'),{lineNumbers:1,matchBrackets:1,mode:'javascript'});});</script><textarea id="code"></textarea>

CSS

#code {
    display: none;
}

.CodeMirror, .CodeMirror-scroll {
    height: 100% !important;            
}

CoffeeScript

### variables ###
x = 5
y = 5

### operators ###
console.log x + y
console.log "#{x} + #{y} = " + x + y
console.log "#{x} + #{y} = " + (x + y)
console.log z?
# z ?= x + y
console.log z

### functions ###
fnNoSplats = (arg1, arg2, arg3, arg4) ->
  console.log arguments

fnSplats = (args...) ->
  console.log args
  
fnNoSplats 'testing...', 1, 2, 3
fnSplats   'testing...', 1, 2, 3
  
### classes, iheritance, and super ###
class Person
  constructor: (@name) ->
  
  greet: (family) ->
    console.log "Hello, #{@name}!"
    console.log "You're Family!!!" if family
    
class Family extends Person
  @siblings = ['Meghan', 'Brian']
  
  greet: ->
    super if @name in @siblings then true else false 

mike   = new Family "Mike"
meghan = new Family "Meghan"

mike.greet()
meghan.greet()