DEV Community

Discussion on: Javascript OOP-1 (Classes and Objects) -

Collapse
 
enetojara profile image
Ernesto Jara Olveda • Edited

So you Is saying that a class could include methods or constructor. So a class can AND cannot have constructor? Interesting.

heroName and powerName are adjectives, so they should be attributes of the class, not local variables of a methods.

Collapse
 
shubhamtiwari909 profile image
Shubham Tiwari

A class body can contain methods and constructors isn't right ?

Collapse
 
enetojara profile image
Ernesto Jara Olveda

The constructor() method is called automatically when a class is initiated, and it has to have the exact name "constructor", in fact, if you do not have a constructor method, JavaScript will add an invisible and empty constructor method.

Thread Thread
 
shubhamtiwari909 profile image
Shubham Tiwari

By saying include , i meant the same
An invisible constructor will get invoked automatically if we don't create one