Get the name of an object's type

前端 未结 20 2663
忘掉有多难
忘掉有多难 2020-11-21 22:37

Is there a JavaScript equivalent of Java\'s class.getName()?

20条回答
  •  深忆病人
    2020-11-21 22:59

    Use constructor.name when you can, and regex function when I can't.

    Function.prototype.getName = function(){
      if (typeof this.name != 'undefined')
        return this.name;
      else
        return /function (.+)\(/.exec(this.toString())[1];
    };
    

提交回复
热议问题