Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 !

like image 368
Marc-André Lafortune Avatar asked Jun 03 '12 04:06

Marc-André Lafortune


2 Answers

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.

like image 170
Jeff Dickey Avatar answered Oct 25 '22 03:10

Jeff Dickey


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

like image 31
Niko Avatar answered Oct 25 '22 01:10

Niko