Get the name of an object's type

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

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

20条回答
  •  春和景丽
    2020-11-21 23:06

    If anyone was looking for a solution which is working with jQuery, here is the adjusted wiki code (the original breaks jQuery).

    Object.defineProperty(Object.prototype, "getClassName", {
        value: function() {
            var funcNameRegex = /function (.{1,})\(/;
            var results = (funcNameRegex).exec((this).constructor.toString());
            return (results && results.length > 1) ? results[1] : "";
        }
    });
    

提交回复
热议问题