Search code examples
coffeescriptintrospection

Getting the name of a coffeescript class?


Will the following alert "Foo" in all browsers, even when minified?

class Foo

alert(Foo.name)

Nothing is stated in the doc, I know that IE has problems with function names, and I'm confused with the many issues opened about this, like any of these issues !


Solution

  • That may depend on which version of the CoffeeScript compiler you're using. In the lastest stable release (1.3.3), a "name" property isn't generated by default.

    class Foo
    

    compiles into

    var Foo;
    Foo = (function() {
      function Foo() {}
      return Foo;
    })();
    

    Since the name property is non-standard and currently not supported by the IE, you cannot really rely on it cross-browser. Detailed information about this are available at the MDN: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/Name