Skip to content

no way of getting constructor name #714

@frank06

Description

@frank06

I believe it's currently not possible to obtain a class' constructor name.

I'd like to do the following:

class Persistence
  save: () =>
    # apply some string manipulation wizardry and persist to database
    console.log "Saving #{extractTableName @constructor.toString()}"

class Animal extends Persistence
class Horse extends Animal

(new Animal).save()
(new Horse).save()

Right now @constructor.toString() will not yield the name because the constructor is an anonymous function assigned to a variable - and therefore doesn't have a name.

Would it be feasible to name the constructor function, in addition to the assignment, like so?

Horse = function Horse() {
  return Animal.apply(this, arguments);
};

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions