1

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 >> ""

How do you translate the js code above to coffee?

Yad Smood
  • 2,752
  • 2
  • 24
  • 37
  • 1
    a) `.name` is non-standard, you shouldn't use it anyway. b) why don't you use `class` syntax for constructors (you still, for some reason, invoke `Foo` with `new`)? – Bergi May 17 '14 at 21:50
  • @Bergi See comment in the second line of javascript. – Yad Smood May 17 '14 at 22:00
  • Which one, the "*I need other private methods*"? How would that prevent you from using `class` syntax? – Bergi May 17 '14 at 22:04
  • @Bergi I update my question, please see it. See the a_public_method part. – Yad Smood May 17 '14 at 22:18

2 Answers2

2

How do you translate the js code above to coffee?

You put all the code that resides in the constructor function Foo in the constructor of a Foo class:

class Foo
  # what you put here *is* static
  constructor: ->
    # it's an instance member, so it goes into the constructor
    non_static_private_member = 10;

    @a_public_method = ->
      non_static_private_member = 1
      return

    console.log(non_static_private_member++);

a = new Foo(); # output >> "10"
b = new Foo(); # output >> "10"
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

CoffeeScript will only generate named functions when used with class syntax. Basically, your first snippet will translate into

var Foo;
Foo = (function() {
    var non_static_private_member;
    non_static_private_member = 10;
    function Foo() {
        console.log(non_static_private_member++);
    }
return Foo;
})();

while second will become

var Foo;
Foo = function() {
    var non_static_private_member;
    non_static_private_member = 10;
    return console.log(non_static_private_member++);
};

This answer explains the reasoning behind such code generation a bit.

For private fields, you can do a trick similar to JS:

class Foo
   constructor: ->
       non_static_private_member = 10
       console.log(non_static_private_member++)
       @some = -> console.log(non_static_private_member)

a = new Foo  # output >> "10"
b = new Foo  # output >> "10"
a.some() # output >> "11"
console.log a.constructor.name # output >> "Foo"
Community
  • 1
  • 1
  • I've already known the details why coffee will generate something like that. I just need some code that will output the value that I expected, and have a non-static private property that can be accessed in the object scope, and the constructor behaves right. – Yad Smood May 17 '14 at 22:07
  • Why not move your `non_static_private_member = 10` into the constructor then? That would yield you same results as with your JS snippet. – YellowAfterlife May 17 '14 at 22:14
  • I update my question, please see it. See the `a_public_method` part. – Yad Smood May 17 '14 at 22:17