If I want to get a function's name by constructor.name
.
For example, in js we can do something like this:
var Foo = function Foo() {
// I need other public methods can also access this private property.
var non_static_private_member = 10;
this.a_public_method = function() {
non_static_private_member = 1;
}
console.log(non_static_private_member++);
}
var a = new Foo(); // output >> "10"
var b = new Foo(); // output >> "10"
console.log(a.constructor.name); // output >> "Foo"
But in coffee the b = new Foo
can't output 10
, it output 11
:
class Foo
non_static_private_member = 10
constructor: ->
console.log(non_static_private_member++)
a = new Foo # output >> "10"
b = new Foo # output >> "11"
console.log a.constructor.name # output >> "Foo"
But if I declare coffee like this, the output of a.constructor.name
is wrong:
Foo = ->
non_static_private_member = 10
console.log(non_static_private_member++)
a = new Foo # output >> "10"
b = new Foo # output >> "10"
console.log a.constructor.name # output >> ""