Module: legio/struct

legio/struct

(require("legio/struct"))(keys, prototypeopt, typeDefinitionopt) → {constructor}

Easily generates a constructor based on given keys, prototype and type definition.
Parameters:
Name Type Attributes Description
keys Array.<String> If just keys are given, the array is not necessary.
prototype Object <optional>
typeDefinition Object <optional>
An object passed to the `type()` function.
Source:
Returns:
Type
constructor
Examples
// A basic struct
var Point = struct("x", "y", "z");

var p = new Point(1, 2, 3); // p.x = 1; p.y = 2; p.z = 3;
// Using the type definition
var TypedPoint = struct(
  ["x", "y", "z"], null,
  { x: Number, y: Number, z: type.OR(type.UNDEFINED, Number) }
);

var
p1 = new TypedPoint(1, 2, 3), // Okay
p2 = new TypedPoint(1, 2), // Okay
p3 = new TypedPoint(1, "hello"); // Error
Source: