DEV Community

GaurangDhorda
GaurangDhorda

Posted on

constructor.name is not working in production

In angular do not use constructor.name in production directly. When
ng build --prod fires to create production build of Angular app at the same time many techniques applied to create minimum size of build and improve performance of application. At this process webpack minifiere converts class name to short name, and thus you can not get real class name by constructor.name in production rather we get minified name of class and that is different. So to solve this we have to save class name in class property when creating class instance. This way we can maintain our class name logic in production build.

Below image is working fine in Development mode.

Alt Text

Below image is Working fine production mode. so we have changed our code for getting class name by setting up class proterties of class name.

Alt Text

Top comments (4)

Collapse
 
gaponow profile image
Гапонов Ілля

Also you can use instanceof operator instead of component.constructor.name

component.constructor.name === 'MyComponent'
--->>>
component instanceof MyComponent

Collapse
 
gfhzhang profile image
shuozhang

instanceof not usefull

Collapse
 
hatemalhasani profile image
HatemAlhasani

I think you need to change the producation settings for "sourceMap" to true

Collapse
 
gaurangdhorda profile image
GaurangDhorda

Source maps are helpful for debugging code, and this would not needed here in this regard. check out updated post here.