最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I specify .constructor.name on a custom object? - Stack Overflow

programmeradmin5浏览0评论

This question must be rather simple indeed, but I cannot google or duckduckgo the answer, probably because 99% of beginner tutorials involve creating a Person object with a name property (which is unrelated to the constructor.name but pollutes the search results).

Instances of Object Objects can be detected using instance.constructor.name:

var foo = new Object;
foo.constructor.name; // "Object"

How do I set the constructor name on my own objects?

var Bar = function(){};
var baz = new Bar;
baz.constructor.name // "" <- Empty string

This question must be rather simple indeed, but I cannot google or duckduckgo the answer, probably because 99% of beginner tutorials involve creating a Person object with a name property (which is unrelated to the constructor.name but pollutes the search results).

Instances of Object Objects can be detected using instance.constructor.name:

var foo = new Object;
foo.constructor.name; // "Object"

How do I set the constructor name on my own objects?

var Bar = function(){};
var baz = new Bar;
baz.constructor.name // "" <- Empty string
Share Improve this question asked Jun 18, 2013 at 12:50 RedsandroRedsandro 11.4k15 gold badges80 silver badges111 bronze badges 2
  • 1 You could just test if( baz.constructor === Bar ), .name is not that reliable – Esailija Commented Jun 18, 2013 at 13:01
  • True, +1, or similarly you can test baz instanceof Bar, but the reliability of .name is not a problem when you develop for only one javascript engine (e.g. nodejs). I prefer to know how this is supposed to work. nnnnnn answered this to the full extend of the question. :) – Redsandro Commented Jun 18, 2013 at 13:11
Add a ment  | 

3 Answers 3

Reset to default 10

Instead of defining your constructor as a variable Bar that is set to an anonymous function, you can either do this:

function Bar() {}

Or this:

var Bar = function ConstructorNameHere() {};

Where the ConstructorNameHere name can actually be Bar again:

var Bar = function Bar() {};

(That should work in Chrome and FF, not sure but not very hopeful about IE.)

Important warning:

If you use a JS minifier then the name will be likely optimized away and suddenly constructor.name bees n instead of the object name you were expecting.

This can be especially hard to debug if it's only enabled in production.

I wanted to make an answer to consolidate things from answers and ments above.

The most deterministic way to have the class name work (especially after minification) is to set a name property on the class itself. All of the following are equivalent:

// option 1
function MyClass() {}
MyClass.name = 'MyClass'

// option 2
class MyClass {}
MyClass.name = 'MyClass'

// option 3 (preferred)
class MyClass {
  static name = 'MyClass'
}

If you want the class name to be dynamic, you create a factory:

function classFactory(className) {
    return class {
        static name = className;
    }
}

const MyClass = classFactory('MyClass')
发布评论

评论列表(0)

  1. 暂无评论