chain together lazy constructors
Construct prototypes and constructors in a lazy style. Chain prototypes and constructors together.
There's a lot of jargon here around constructors and 'lazy', so a use-case example is going to be more helpful here.
A Crockford-style Super Constructor (if you don't construct objects this way you may not want this script):
var myproto =prop1 : 'prop1'console.logthisprop1;;varvar that = Objectcreatemyproto;thatprop1 = optsprop1;return that;;var myobj = myconstructor prop1 : 'myprop1' ;myobjmeth1; // myprop1
When building something big I needed two things:
Lazy definition (then caching) of prototype and constructor.
lazycon lets you define prototypes and constructor functions dynamically. Expensive definitions are deferred until needed and the result is cached.
The ability to chain prototypes and constructors
lazycon lets you join multiple prototype and constructor definitions to return a new constructor. Why?:
augmented prototype objects. You might have a collection of 'views' scripts. Each view inherits methods from one prototype for content, container and error rendering. Other views need prototype methods unique to their function -a view managing a form or one that manages a form control.
A simple approach defines all view properties on one prototype or uses a deep inheritance chain to build more specific prototypes (slower property lookup).
lazycon creates one prototype from multiple objects with one layer of inheritence. The cached result is passed to the constructor.
augmented constructor methods. An augmented prototype object needs an augmented constructor to avoid repeated property definitions in each constructor.
lazycon passes the constructed object through multiple constructors. Define a prototype and a constructor in one place, then create a new constructor and use corresponding parts from a predefined constructor.
Build a constructor
// staticvar constructor1 = lazycondefProtoFnpropertya : ''property1 : ''return thisproperty1;defConstructFnmepropertya = 'propa';meproperty1 = 'thisproperty1';mearg1 = arg1;return me;;// or dynamicvar constructor1 = lazycondefProtoFnreturnpropertya : ''property1 : ''return thisproperty1;;defConstructFnmepropertya = 'propa';meproperty1 = 'thisproperty1';mearg1 = arg1;return me;;
Build an inheriting constructor
var constructor2 = lazyconconstructor1defProtoFnarg2 : ''property2 : ''return thisproperty2;defConstructFnmepropertya = 'propartyb';meproperty2 = 'thisproperty2';mearg2 = arg2;return me;;
Use the constructor
var constructedobj = constructor2getNew'arg1' 'arg2';console.logconstructedobjpropertya; // propertybconsole.logconstructedobjarg1; // arg1console.logconstructedobjmethod1; // thisproperty1console.logconstructedobjmethod2; // thisproperty2
The constructor function must return the first parameter.
The object is constructed in an obvious way -prototype properties are mapped to an object in a first-in sequential order. Constructors are applied to the result in a first-in sequential order.
A new constructor may inherit from multiple
lazycon constructors and will use their cached prototype definitions.
var newconstructor = lazyconconstructor1constructor2constructor3;
lazycon may be downloaded directly or installed through
$ npm install lazycon
$ git clone
to run tests, use
npm test from a shell.
$ npm test
(The MIT License)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.