Computer >> Computer tutorials >  >> Programming >> Javascript

Smart / self-overwriting / lazy getters in javaScript?


Getters defines a property of an object, but the property's value is not calculated. It gets calculated when it is accessed. Therefore, a getter delays the cost of calculating the value until the value is needed.

To lazify or delay the calculation are smart or memoized getters.You can also cache it to access it later. The value is only calculated if the getter is called. For the next access, it is then cached; therefore so successive accesses without recalculating the value.

Do not use a lazy getter for a property whose value is to be changed. The reason as discussed above is that the getter won’t recalculate the value.