lazyval
A simple way to define lazy properties to a generic object or a prototype.
Compatible with mostly every browser or engine out there: feel free to test it.
function signature
;// returns the generic Object; ;// for multiple lazy properties at once;
lazy examples via object and properties
var o = ; // only once needed ...ouid; // object:27364279
lazy examples via inheritance and 3 arguments
{} // note lazyval.protolazyval; var a1 = a2 = ; a1expensive; // 0.346731865a1expensive === a1expensive; // truea2expensive === a1expensive; // falsea2expensive; // 0.874216789 // clean updelete a1expensive;a1expensive; // 0.4567891a1expensive === a1expensive; // true
lazyval.direct VS lazyval.proto
The difference is that lazyval.proto
will not actually replace at runtime the property to the prototype object itself but only to inheriting isntances. lazyval.direct
is somehow more greedy here because an access to the prototype object could affect all inherited instances.
For classes and prototypal inheritance, lazyval.proto
is recommended.