This JavaScript works great, because the JS runtime parses everything for declarations before running statements.
try {
function Test() {
this.speak = function() { alert('From Test!') }
}
test = new Test
test.speak()
} catch (error) {
alert(error);
}
try {
secondtest = new SecondTest
secondtest.speak()
function SecondTest() {
this.speak = function() { alert('From SecondTest!') }
}
} catch (error) {
alert(error)
}
// Alert: 'From Test!'
// Alert: 'From SecondTest!'
However, the corresponding CoffeeScript does not work when I create an instance of a class above its declaration:
try
class Test
speak: -> alert 'From Test!'
test = new Test
test.speak()
catch error
alert error
try
secondtest = new SecondTest
secondtest.speak()
class SecondTest
speak: -> alert 'From SecondTest!'
catch error
alert error
// Alert: 'From Test!'
// Alert: 'TypeError: undefined is not a function'