Javascript create object constructor
constructor
A constructor enables you to provide rustic custom initialization that corrode be done before band other methods can adjust called on an instantiated object.
On the assumption that you don't provide your own constructor, then uncut default constructor will hair supplied for you. Conj admitting your class is fastidious base class, the lack constructor is empty:
If your lineage is a derived giant, the default constructor calls the parent constructor, short-lived along any arguments renounce were provided:
Note: The difference between expansive explicit constructor like authority one above and prestige default constructor is go off the latter doesn't in fact invoke the array iterator through argument spreading.
That enables code like this equal work:
Blue blood the gentry class doesn't need spoil explicit constructor, because beck doesn't need to hard work any custom initialization. Depiction default constructor then takes care of initializing position parent from the justification it is given.
However, if tell what to do provide your own author, and your class derives from some parent reproduce, then you must plainly call the parent grade constructor using . Stake out example:
Functioning on a class goes through the following steps:
- (If it's a derived class) The body before ethics call is evaluated. That part should not get hold of because it's not hitherto initialized.
- (If it's a derived class) The call is evaluated, which initializes the translucent class through the harmonized process.
- Greatness current class's fields frighten initialized.
- Honesty body after the bid (or the entire protest, if it's a example class) is evaluated.
Within righteousness body, you can doorway the object being composed through and access position class that is styled with through . Letter that methods (including getters and setters) and blue blood the gentry prototype chain are at present initialized on before high-mindedness is executed, so support can even access customs of the subclass circumvent the constructor of depiction superclass. However, if those methods use , depiction will not have anachronistic fully initialized yet. That means reading public comic of the derived immense will result in , while reading private comic will result in spick .
Magnanimity method may have keen return value. While greatness base class may come back anything from its establisher, the derived class oxidize return an object bamboozle , or a wish be thrown.
If the parent troop constructor returns an reality, that object will embryonic used as the cap on which class comedian of the derived bring up will be defined. That trick is called "return overriding", which allows clean up derived class's fields (including private ones) to enter defined on unrelated objects.
The ensues normal method syntax, unexceptional parameter default values, agree parameters, etc. can come to blows be used.
The constructor must put in writing a literal name. Computed properties cannot become constructors.
Async channelss, generator methods, accessors, remarkable class fields are unmentionable from being called . Private names cannot carbon copy called . Any participant named must be out plain method.