{"bugs":{"url":"https://github.com/fasttime/Polytype/issues"},"dist":{"shasum":"845123ee6191111d2a16c8e8345a89c2d8280120","tarball":"https://registry.npmjs.org/polytype/-/polytype-0.6.0-beta.1.tgz","fileCount":12,"integrity":"sha512-A+gBq1xkFB7ZAKSwzm7cP8tB1feAHZX87IfkcEBXVq3hCRFWUhyMuJ5Wr5Pw6Bq8o/ulkjXrDzE1NiouPxJFEg==","signatures":[{"sig":"MEYCIQClEnOae9B5T6yPvffLJaEd2g4ABsSbpl/oUlCYelTL/QIhANDoWbNT2+aKE3jcX1FhbmXNUvoqXZestCSQ6FJdYHMb","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":91269,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJdpZJaCRA9TVsSAnZWagAASjgP/2gnfocAe4lhUBG5nxwV\nubkUY1acPFGzrs4M44ukENga3kVZfKaF6Jh5nfz4P3flSrAfTDclVWbbYUHI\n8vNUSsrXwx7juteTLvh1ikBao27J96avAxfCotjoF/BdaEwTw5CWey1YsyAC\nKNXY5xyEgfUhshxK1M7csLOhdstIelt6g+Q2NUKCsTzCiBwibTUpLpaKgkbt\nKgXa/Dfw+uL1mc+qDoTJfZt+7ZBjEAviZzvD21riiraahfMvLCBpyXGFwcFb\nzq5FbBR69/mJpi+0cVnusE6y4WUG3UoYWkVoI6VlGw3xSsOXDGWy9l8D49Iu\n2nJBnGuEx/7OS6YjT3fXFLymtTUVikN27y4s9p6rDlCy8ngYeGNf88vJ9tUw\n3h2DvXD7/cgy1qGJz4ObPQsmUpJSBtfKRO/gLICYLMpyb+SNgm20YZpIBNe+\nIFBOFIpctC5Lusx+EAYIFduvTl3Vbrvfpo24CRjAo8+P1QLK6wc9IwdcPWvl\n5GRn3TeRJ1fjbrb7e7/mZ97Yw0cI6X7kWwQMI7t8ASDfedpWiu48RMxPeGUc\nABZu55W01rR6rcIBPxowFnPQd+kzHSqVtMO+3Q7NnMhfheYePVo9c3xIXPkT\nGD3DwBVSIebWbo5OzwcZenV7+8OaGHskpc8qG75HeKF8UM56WG86s3/QY9s1\n2zUi\r\n=vpnW\r\n-----END PGP SIGNATURE-----\r\n"},"main":"lib/polytype.cjs","name":"polytype","types":"lib/polytype-module.d.ts","author":{"url":"https://github.com/fasttime","name":"Francesco Trotta","email":"ft@fasttime.org"},"module":"lib/polytype.mjs","readme":"# Polytype · [![npm version][npm badge]][npm url]\n\n**If you are upgrading from a previous version, note that usage has changed.**\n**Change your imports to `\"polytype/global\"` to keep using the script build.**\n**Check the [Setup Instructions](#setup-instructions) for details.**\n\n---\n\n**Dynamic multiple inheritance for JavaScript and TypeScript. Without mixins.**\n\n**Polytype** is a library that adds support for dynamic\n[multiple inheritance](https://en.wikipedia.org/wiki/Multiple_inheritance) to JavaScript and\nTypeScript with a simple syntax.\n“Dynamic” means that changes to base classes at runtime are reflected immediately in all derived\nclasses just like programmers would expect when working with single prototype inheritance.\n\nPolytype uses\n[proxies](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy)\nalong with other relatively new language features to provide multiple inheritance.\nSome of these features are not yet well supported in all browsers.\nAs of today, Polytype runs in **current versions of Chrome, Firefox, Safari, Opera and in Node.js**.\nAs JavaScript support in other browsers improves, Polytype will start to run in those browsers, too.\n\n## Contents\n\n- [Contents](#contents)\n- [Features](#features)\n- [Setup Instructions](#setup-instructions)\n  * [In Node.js](#in-nodejs)\n  * [In the browser](#in-the-browser)\n- [Usage](#usage)\n  * [Inheriting from multiple base classes](#inheriting-from-multiple-base-classes)\n  * [Using methods and properties from multiple base classes](#using-methods-and-properties-from-multiple-base-classes)\n  * [Static methods and properties](#static-methods-and-properties)\n  * [Invoking multiple base constructors](#invoking-multiple-base-constructors)\n  * [`instanceof`](#instanceof)\n  * [`in`](#in)\n  * [`isPrototypeOf`](#isprototypeof)\n  * [Finding the base classes](#finding-the-base-classes)\n  * [Dynamic base class changes](#dynamic-base-class-changes)\n- [TypeScript support](#typescript-support)\n- [Caveats](#caveats)\n  * [`for...in` iterations](#forin-iterations)\n  * [Member resolution order](#member-resolution-order)\n  * [Ambiguous protected instance members](#ambiguous-protected-instance-members)\n- [Compatibility](#compatibility)\n\n## Features\n\n* Python style multiple inheritance\n* Works in Node.js and in most browsers\n* Full TypeScript support\n* Zero dependencies\n* Qualified or unqualified access to all base class features\n  * constructors\n  * methods, getters and setters – both static and nonstatic\n  * value properties on base classes and base instance prototypes\n  * public [class fields](https://github.com/tc39/proposal-class-fields) (in engines that support\n    them)\n* `in`, `instanceof` and `isPrototypeOf` integration\n\n## Setup Instructions\n\nPolytytpe is available in two flavors: a module build (comprising CommonJS and ECMAScript modules)\nwith exported definitions, and a script build where all definitions are accessible through global\nobjects.\nApart from this, both builds provide the same features and are available in the standard package.\n\nYou are free to choose whichever build of Polytype best fits your requirements, but make sure to\nremain consistent throughout your application: mixing the module and the script builds together is\nnot recommended and may produce unexpected results.\n\n### In Node.js\n\nIf you are using Node.js, you can install Polytype with [npm](https://www.npmjs.org).\n\n```console\nnpm install polytype\n```\n\nThen you can import it in your code like any module.\n\n```js\nconst { classes } = require(\"polytype\"); // CommonJS syntax\n```\nor\n```js\nimport { classes } from \"polytype\"; // ECMAScript module syntax\n```\n\nIn TypeScript you can also import certain types where necessary.\n\n```ts\nimport { SuperConstructorInvokeInfo } from \"polytype\";\n```\n\nAlternatively, you can import the script build at the start of your application and access Polytype\ndefinitions through global objects.\n\n```js\nrequire(\"polytype/global\"); // CommonJS syntax\n```\nor\n```js\nimport \"polytype/global\"; // ECMAScript module syntax\n```\n\n### In the browser\n\nIn an HTML‐based application, the script build of Polytype can be simply embedded.\nJust download [polytype.min.js][polytype.min.js] from GitHub and include it in your HTML file.\n\n```html\n<script src=\"polytype.min.js\"></script>\n```\n\nAlternatively, you can hotlink the current stable version using a CDN of your choice.\n\n```html\n<script src=\"https://cdn.statically.io/gh/fasttime/Polytype/0.6.0-beta.1/lib/polytype.min.js\"></script>\n```\n\nIf your browser application already uses ECMAScript modules, you can also import the module build\n(“.mjs”) in contexts where Polytype specific definitions like `classes` are required.\nThis has the advantage to avoid possible naming conflicts on global objects.\n\n```js\nimport { classes } from \"https://cdn.statically.io/gh/fasttime/Polytype/0.6.0-beta.1/lib/polytype.min.mjs\";\n```\n\n## Usage\n\n### Inheriting from multiple base classes\n\nFor example, declare a derived class `ColoredCircle` that inherits from both base classes `Circle`\nand `ColoredObject`.\n\n```js\nclass Circle\n{\n    constructor(centerX, centerY, radius)\n    {\n        this.moveTo(centerX, centerY);\n        this.radius = radius;\n    }\n    get diameter() { return this.radius * 2; }\n    set diameter(diameter) { this.radius = diameter / 2; }\n    moveTo(centerX, centerY)\n    {\n        this.centerX = centerX;\n        this.centerY = centerY;\n    }\n    toString()\n    {\n        return `circle with center (${this.centerX}, ${this.centerY}) and radius ${this.radius}`;\n    }\n}\n\nclass ColoredObject\n{\n    constructor(color) { this.color = color; }\n    static areSameColor(obj1, obj2) { return obj1.color === obj2.color; }\n    paint() { console.log(`painting in ${this.color}`); }\n    toString() { return `${this.color} object`; }\n}\n\nclass ColoredCircle\nextends classes(Circle, ColoredObject) // Base classes as comma‐separated params\n{\n    // Add methods here.\n}\n```\n\n### Using methods and properties from multiple base classes\n\n```js\nconst c = new ColoredCircle();\n\nc.moveTo(42, 31);\nc.radius = 1;\nc.color = \"red\";\nconsole.log(c.centerX, c.centerY);  // 42, 31\nconsole.log(c.diameter);            // 2\nc.paint();                          // \"painting in red\"\n```\n\nAs usual, the keyword `super` invokes a base class method or property accessor when used inside a\nderived class.\n\n```js\nclass ColoredCircle\nextends classes(Circle, ColoredObject)\n{\n    paint()\n    {\n        super.paint(); // Using method paint from some base class\n    }\n}\n```\n\nIf different base classes include a member with the same name, the syntax\n```js\nsuper.class(DirectBaseClass).member\n```\ncan be used to make the member access unambiguous.\n\n```js\nclass ColoredCircle\nextends classes(Circle, ColoredObject)\n{\n    toString()\n    {\n        // Using method toString from base class Circle\n        const circleString = super.class(Circle).toString();\n        return `${circleString} in ${this.color}`;\n    }\n}\n```\n\nMore generally, `super.class(DirectBaseClass)[propertyKey]` can be used to reference a property of a\nparticular base class in the body of a derived class.\n\n**Note:**\nIn TypeScript, the syntax described here cannot be used to access protected instance members, so it\nis currently not possible to disambiguate between protected instance members having the same name,\nthe same index or the same symbol in different base classes.\n\n### Static methods and properties\n\nStatic methods and property accessors are inherited, too.\n\n```js\nColoredCircle.areSameColor(c1, c2)\n```\nsame as\n```js\nColoredObject.areSameColor(c1, c2)\n```\n\n### Invoking multiple base constructors\n\nUse arrays to group together parameters for each base constructor in the derived class constructor.\n\n```js\nclass ColoredCircle\nextends classes(Circle, ColoredObject)\n{\n    constructor(centerX, centerY, radius, color)\n    {\n        super\n        (\n            [centerX, centerY, radius], // Circle constructor params\n            [color]                     // ColoredObject constructor params\n        );\n    }\n}\n```\n\nIf you prefer to keep parameter lists associated to their base classes explicitly without relying on\norder, there is an alternative syntax.\n\n```js\nclass GreenCircle\nextends classes(Circle, ColoredObject)\n{\n    constructor(centerX, centerY, radius)\n    {\n        super\n        (\n            { super: ColoredObject, arguments: [\"green\"] },\n            { super: Circle, arguments: [centerX, centerY, radius] }\n        );\n    }\n}\n```\n\nThere is no need to specify an array of parameters for each base constructor.\nIf the parameter arrays are omitted, the base constructors will still be invoked without parameters.\n\n```js\nclass WhiteUnitCircle\nextends classes(Circle, ColoredObject)\n{\n    constructor()\n    {\n        super(); // Base constructors invoked without parameters\n        this.centerX    = 0;\n        this.centerY    = 0;\n        this.radius     = 1;\n        this.color      = \"white\";\n    }\n}\n```\n\n### `instanceof`\n\nThe `instanceof` operator works just like it should.\n\n```js\nconst c = new ColoredCircle();\n\nconsole.log(c instanceof Circle);           // true\nconsole.log(c instanceof ColoredObject);    // true\nconsole.log(c instanceof ColoredCircle);    // true\nconsole.log(c instanceof Object);           // true\nconsole.log(c instanceof Array);            // false\n```\n\nIn pure JavaScript, the expression\n```js\nB.prototype instanceof A\n```\ndetermines if `A` is a base class of class `B`.\n\nPolytype takes care that this test still works well with multiple inheritance.\n\n```js\nconsole.log(ColoredCircle.prototype instanceof Circle);         // true\nconsole.log(ColoredCircle.prototype instanceof ColoredObject);  // true\nconsole.log(ColoredCircle.prototype instanceof ColoredCircle);  // false\nconsole.log(ColoredCircle.prototype instanceof Object);         // true\nconsole.log(Circle.prototype instanceof ColoredObject);         // false\n```\n\n### `in`\n\nThe `in` operator determines whether a property is in an object or in its prototype chain.\nIn the case of multiple inheritance, the prototype “chain” looks more like a directed graph, yet the\nfunction of the `in` operator is the same.\n\n```js\nconst c = new ColoredCircle();\n\nconsole.log(\"moveTo\" in c); // true\nconsole.log(\"paint\" in c);  // true\n```\n\n```js\nconsole.log(\"areSameColor\" in ColoredCircle);   // true\nconsole.log(\"areSameColor\" in Circle);          // false\nconsole.log(\"areSameColor\" in ColoredObject);   // true\n```\n\n### `isPrototypeOf`\n\n`isPrototypeOf` works fine, too.\n\n```js\nconst c = new ColoredCircle();\n\nconsole.log(Circle.prototype.isPrototypeOf(c));         // true\nconsole.log(ColoredObject.prototype.isPrototypeOf(c));  // true\nconsole.log(ColoredCircle.prototype.isPrototypeOf(c));  // true\nconsole.log(Object.prototype.isPrototypeOf(c));         // true\nconsole.log(Array.prototype.isPrototypeOf(c));          // false\n```\n\n```js\nconsole.log(Circle.isPrototypeOf(ColoredCircle));               // true\nconsole.log(ColoredObject.isPrototypeOf(ColoredCircle));        // true\nconsole.log(ColoredCircle.isPrototypeOf(ColoredCircle));        // false\nconsole.log(Object.isPrototypeOf(ColoredCircle));               // false\nconsole.log(Function.prototype.isPrototypeOf(ColoredCircle));   // true\n```\n\n### Finding the base classes\n\nIn single inheritance JavaScript, the direct base class of a derived class is obtained with\n`Object.getPrototypeOf`.\n\n```js\nconst DirectBaseClass = Object.getPrototypeOf(DerivedClass);\n```\n\nIf a class has no explicit `extends` clause, `Object.getPrototypeOf` returns `Function.prototype`,\nthe base of all classes.\n\nOf course this method cannot work with multiple inheritance, since there is no way to return\nmultiple classes without packing them in some kind of structure.\nFor this and other use cases, Polytype exports the function `getPrototypeListOf`, which can be used\nto get an array of direct base classes given a derived class.\n\n```js\nconst { getPrototypeListOf } = require(\"polytype\"); // Or some other kind of import.\n\nfunction getBaseNames(derivedClass)\n{\n    return getPrototypeListOf(derivedClass).map(({ name }) => name);\n}\n\nconsole.log(getBaseNames(ColoredCircle));   // [\"Circle\", \"ColoredObject\"]\nconsole.log(getBaseNames(Int8Array));       // [\"TypedArray\"]\nconsole.log(getBaseNames(Circle));          // [\"\"] i.e. [Function.prototype.name]\n```\n\nIf you use the script build of Polytype, no functions will be exported.\nInstead, `getPrototypeListOf` will be defined globally as `Object.getPrototypeListOf`.\n\n### Dynamic base class changes\n\nIf a property in a base class is added, removed or modified at runtime, the changes are immediately\nreflected in all derived classes.\nThis is the magic of proxies.\n\n```js\nconst c = new ColoredCircle();\n\nCircle.prototype.sayHello = () => console.log(\"Hello!\");\nc.sayHello(); // \"Hello!\"\n```\n\n## TypeScript support\n\nPolytype has built‐in TypeScript support: you can take advantage of type checking while working with\nmultiple inheritance without installing any additional packages.\nIf you are using an IDE that supports TypeScript code completion like Visual Studio Code, you will\nget multiple inheritance sensitive suggestions as you type.\nA TypeScript version of the `ColoredCircle` sample code above can be found in\n[ColoredCircle.ts](https://github.com/fasttime/Polytype/blob/0.6.0-beta.1/example/ColoredCircle.ts)\nin the example folder.\n\n## Caveats\n\nNeither JavaScript nor TypeScript offer native support for multiple inheritance of any kind.\nPolytype strives to make up for this deficiency, but some important limitations remain.\n\n### `for...in` iterations\n\nIn plain TypeScript, without Polytype, a `for...in` iteration over a class constructor will\nenumerate not only names of static fields defined on that class, but also names of static fields\ndefined on all base classes in its prototype chain.\n\n```ts\nclass FooClass\n{\n    static foo = \"foo\";\n}\n\nclass BarClass extends FooClass\n{\n    static bar = \"bar\";\n}\n\nfor (const name in BarClass)\n    console.log(name); // Prints \"bar\" and \"foo\".\n```\n\nAdditionally, some newer JavaScript engines have implemented support for\n[static public fields](https://github.com/tc39/proposal-static-class-features#static-public-fields),\nwhich work much like in TypeScript, but without transpiling.\nEven in JavaScript, a `for...in` iteration over a class constructor will include names of static\nfields defined on base classes.\n\nAnyway, this behavior breaks with Polytype inheritance on static fields: names of static fields\ndefined on base classes are not enumerated by `for...in` statements if the inheritance line crosses\nthe constructor of a class in some `extends classes(...)` clause.\n\n```ts\nclass BazClass extends classes(FooClass)\n{\n    static baz = \"baz\";\n}\n\nfor (const name in BazClass)\n    console.log(name); // Prints just \"baz\".\n```\n\nFor this reason, and because generally [better alternatives exist][Why Use for...in?], iterating\nover Polytype classes and their derived classes with `for...in` is not recommended.\n\n### Member resolution order\n\nMultiple base classes may expose members with the same name, the same index or the same symbol.\nWhen this happens, any unqualified access to one of those members will have to determine the\nimplementation to be used.\nThe approach taken by Polytype is to pick the implementation found in the first direct base class\nthat contains the (possibly inherited) member.\n\n```js\nclass A\n{ }\n\nclass B\n{\n    whoAmI() { console.log(\"B\"); }\n}\n\nclass C\n{\n    whoAmI() { console.log(\"C\"); }\n}\n\nclass ABC extends classes(A, B, C)\n{ }\n\nconst abc = new ABC();\nabc.whoAmI(); // Prints \"B\".\n```\n\nThis is similar to the depth‐first search algorithm of old‐style classes in Python 2, but it is\ndifferent from the behavior of several other programming languages that support multiple\ninheritance, and it may not match your expectations if you come from a C++ background.\n\n### Ambiguous protected instance members\n\nTypeScript class members can have\n[protected](https://www.typescriptlang.org/docs/handbook/classes.html#understanding-protected)\naccess: protected members are inherited just like public members but can only be accessed in the\nbody of the defining class itself or of a derived class.\nIf a derived class inherits from multiple base classes, it is possible for inherited members in\ndifferent base classes to share the same property key, i.e. the same name, the same index or the\nsame symbol.\nIn this case, Polytype provides the syntax `super.class(DirectBaseClass)[propertyKey]` to specify\nthe base class containing the member to be accessed.\nThis works fine for all public or static members, but results in a compiler error when applied to\nprotected instance members.\n\n```ts\nclass Apple\n{\n    protected id: number;\n}\n\nclass Banana\n{\n    protected id: number;\n}\n\nclass Bananapple extends classes(Apple, Banana)\n{\n    get bananaId()\n    {\n        return super.class(Banana).id; // error TS2446: Property 'id' is protected…\n    }\n}\n```\n\nAs a workaround, you could use an intermediate class to expose the inherited member with a different\nname without making it public.\n\n```ts\nclass BananaProxy extends Banana\n{\n    get bananaId()\n    {\n        return super.id;\n    }\n}\n\nclass Bananapple extends classes(Apple, Banana)\n{ }\n```\n\n## Compatibility\n\nPolytype was successfully tested in the following browsers / JavaScript engines.\n\n* Chrome 63+\n* Firefox 67+\n* Safari 13+\n* Opera 50+\n* Node.js 10.6+\n\nThe minimum supported TypeScript version is 3.5.\n\nIn the current version of the Edge browser, the JavaScript engine Chakra still contains\n[a serious bug](https://github.com/Microsoft/ChakraCore/issues/5883) that can produce incorrect\nresults when the `instanceof` operator is used with bound functions after Polytype has been loaded.\nFor this reason it is recommended not to use Polytype in Edge as long as this issue persists.\n\n[npm badge]: https://badge.fury.io/js/polytype.svg\n[npm url]: https://www.npmjs.com/package/polytype\n[polytype.min.js]:\nhttps://raw.githubusercontent.com/fasttime/Polytype/0.6.0-beta.1/lib/polytype.min.js\n[Why Use for...in?]:\nhttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in#Why_Use_for...in\n","engines":{"node":">=10.6.0"},"gitHead":"5a5395379578199957d8417853079e70e7b3f73e","license":"ISC","scripts":{"test":"test/node-spec-runner.js","build":"npm install && gulp","serve":"./serve.js"},"_npmUser":{"name":"fasttime","email":"github@fasttime.org"},"homepage":"https://github.com/fasttime/Polytype","keywords":["class","classes","es2017","es8","inheritance","multiple-inheritance","proxies","proxy"],"repository":{"url":"git+https://github.com/fasttime/Polytype.git","type":"git"},"_npmVersion":"6.11.3","description":"Dynamic multiple inheritance for JavaScript and TypeScript. Without mixins.","directories":{"lib":"lib","test":"test","example":"example"},"maintainers":[{"name":"fasttime","email":"github@fasttime.org"}],"_nodeVersion":"12.12.0","_hasShrinkwrap":false,"readmeFilename":"readme.md","devDependencies":{"del":"~5.1.0","nyc":"~14.1.1","chai":"~4.2.0","gulp":"~4.0.2","mocha":"~6.2.1","rollup":"~1.23.1","semver":"~6.3.0","mocha-bar":"~0.0.3","handlebars":"~4.4.3","typescript":"~3.6.4","gulp-rename":"~1.4.0","gulp-terser":"~1.2.0","postrequire":"~0.1.1","markdown-toc":"~1.2.0","gulp-fasttime-lint":"~0.17.0"},"_npmOperationalInternal":{"tmp":"tmp/polytype_0.6.0-beta.1_1571131993854_0.09644179718014168","host":"s3://npm-registry-packages"},"_id":"polytype@0.6.0-beta.1","version":"0.6.0-beta.1"}