Using a factory of constructors, I would like those constructors to have different names in the console, also when logging instances of them.
Here is a simplified example of my problem :
// Constructor factory //
function type(name, prototype) {
function Constructor() {}
Constructor.name ; // "Constructor"
// Constructor.name = name won't work properly.
Object.defineProperty(Constructor, 'name', { value:name }) ;
Constructor.prototype = prototype ;
window[name] = Constructor ;
return Constructor ;
}
// Creating constructor and instance //
type('Cat', { name:"", paws:4 }) ;
var chat = new Cat ;
// Tests //
Cat.name ; // "Cat" -> Correct name
Cat ; // Constructor() { ... } -> Incorrect name
chat ; // Constructor {name:"", paws:4} -> Incorrect name
Is there any way to display the right name in this case ?
Tested with the latest version of Chrome (67). In this case I don't want to use class
feature.