Short answer: don't.
This question is probably getting closed as a duplicate. But i'd like to point out that if you're needing to do this kind of trick to solve a problem, you're probably going to introduce another problem by using a trick like that. If the behaviour of a function needs to depend on something (like where is it being called from), make it explicit and use a parameter for that dependency; it's a pattern that everyone will easily understand.
pet = (pet) ->
alert "I was called by #{pet.name} the #{pet.constructor.name}"
class Cat
constructor: (@name) ->
pet @
new Cat 'Felix' # Output: "I was called by Felix the Cat"
That being said, Function#name
is not standard, so you probably shouldn't use that either. But you can safely access a pet's "class" (i.e. its constructor function) by accessing its constructor
property as shown in the example.