Skip to content

Instantly share code, notes, and snippets.

@gkmngrgn
Created October 11, 2011 08:28
Show Gist options
  • Save gkmngrgn/1277567 to your computer and use it in GitHub Desktop.
Save gkmngrgn/1277567 to your computer and use it in GitHub Desktop.
Coffee Compilation
# Copyright (c) 2011, the <your_company> project authors. Please don't see the
# AUTHORS file for details. All rights reserved. Use of this source code is
# governed by a BSD-style license that can be found in the LICENSE file.
# Simple test program invoked with an option to eagerly
# compile all code that is loaded in the isolate.
# VMOptions=--fuu_dart
class HelloCoffeeTest
test_main: ->
console.log "Hello, Coffee man!"
new HelloCoffeeTest().test_main()
(function() {
var HelloCoffeeTest;
HelloCoffeeTest = (function() {
function HelloCoffeeTest() {}
HelloCoffeeTest.prototype.test_main = function() {
return console.log("Hello, Coffee man!");
};
return HelloCoffeeTest;
})();
new HelloCoffeeTest().test_main();
}).call(this);
@jcolebrand
Copy link

Why are you using .call(this) at the end when () would suffice?

@dfellis
Copy link

dfellis commented Oct 11, 2011

That's the CoffeeScript compiler doing it, and .call() is actually undefined behavior in Javascript; normally the browser will do what you're stating, but if you "use strict";, it actually does .call(undefined) and then promptly dies.

That's why.

@sztomi
Copy link

sztomi commented Oct 11, 2011

He isn't, that code is what gets generated from the CoffeeScript above.

@idoan
Copy link

idoan commented Oct 13, 2011

He could call (function(){...})(); instead but in that case "this" inside of (function(){}) will belong to global object. however, in this case it belongs to parent scope.

@sztomi
Copy link

sztomi commented Oct 13, 2011

Guys that code is generated. If anything is worth discussing it is why the coffeescript compiler does that...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment