Skip to content

Constructing objects with generators

thepian edited this page Nov 27, 2011 · 3 revisions

In JavaScript any function can be used to construct an object by calling it with the new operator. That is a great key feature that helps to make JavaScript a simple but powerful language. If you turn the function into a generator you gain extra flexibility, since you can have rules for when a new object is created and when an existing one is reused.

Default Generator

Sometimes it is convenient to have a default behavior for generating instances, making the specification of a generator an optional parameter. If you call Generator() you get a new Object back. So it is equivalent to Generator(Object) or {}.

The Class Equivalent

If you are used to progamming with classes the common generator pattern will seem familiar. First define a constructor,

function Rectangle(width,height) {
    this.width = width;
    this.height = height;
}
Rectangle.prototype.resize = function(width,height) {
    this.width = width;
    this.height = height;
};

By calling Generator(Rectangle) you get a function that will create an object which is an instanceof Rectangle and then call the constructor. Finally the created object will be returned. So in the vanilla version a generator will work just like if you called the constructor with a new operator.

If you want to make objects that are both a Rectangle and a Shape call Generator(Rectangle,Shape). The generator will chain the protoypes of the two functions together so the created instances will be an instanceof both.

Special Constructor Members

You can enhance the basic constructor with several members.

name description
args For passing parameters to base constructors, persistance and configuration.
discarded For objects being discard, see Restricting Object Generation
identifier For identity, see Restricting Object Generation
types Future enhancement for describing data models for MVC.

Generator Housekeeping

The generator will have a couple of properties describing what will be constructed.

name description
args A list of objects defining the arguments to the constructor
types A map of the typed properties of the object generated.
bases A list of base constructors defined on the main constructor or passed to Generator.
variants A map of generator variants by variant name.
variant Method used to set and get a named variant. Unknown variants fall back to the default or base.
mixin Method used to mix variables and functions into the prototype of the generated object.
restrict Method for adding Restricting Object Generation.
info Internal housekeeping
info.symbol When set on a package by a resolver. Refers to the name of the generator.
info.within When set on a package by a resolver. Refers to the package object it is on.
info.package When set on a package by a resolver. Refers to the package name it is on.
info.constructors The exploded list of constructors in the order they will be called. Entry -1 will be the main constructor.
info.singleton The singleton instance when created
info.options Options passed when making the generator.

Clone this wiki locally