Marc-André Lafortune
Marc-André Lafortune

Reputation: 79562

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 !

Upvotes: 9

Views: 8218

Answers (2)

Jeff Dickey
Jeff Dickey

Reputation: 1081

From within any method of class Foo that is included in Foo.prototype, you can insert the line

console.log @constructor.name

and it will write

Foo

to your console log. HTH.

Upvotes: 10

Niko
Niko

Reputation: 26730

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

Upvotes: 8

Related Questions