Lajos Arpad Lajos Arpad - 1 year ago 63
Javascript Question

How to get the name of caller function?

Let us consider this code example:

function Bird() {
var self = this;
this.getType = function() {
return self.type;
function Eagle() {;
this.type = "Eagle";
function Sparrow() {;
this.type = "Sparrow";
function Bat() {;
this.type = "Bat";

var e = new Eagle();

Here the
types are correctly set, but I would like to determine the type on
level. However, to achieve that, I would need to know which function called Bird. However, if I do something like this:

function foo() {
return new Bird();

then I would like to make sure that
results in
. Is this possible?

Answer Source

You don't need to use call:

function Bird(self) {
  var type;
  if (self) {
    type =;
  } else {
    self = this;
  self.getType = function () {return type;};
function Eagle() {
var e = new Eagle();
console.log(e.getType()); // Eagle
console.log(new Bird().getType()); // undefined
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download