https://github.com/rioam2/proxc
🔗 Unlock the extensiblity of ES6 Proxy Objects in Javascript Classes and create beautiful APIs
https://github.com/rioam2/proxc
api-wrapper bracket-operator functors getters index-operator javascript nodejs npm-package operator overloading setters typescript
Last synced: about 2 months ago
JSON representation
🔗 Unlock the extensiblity of ES6 Proxy Objects in Javascript Classes and create beautiful APIs
- Host: GitHub
- URL: https://github.com/rioam2/proxc
- Owner: rioam2
- License: mit
- Created: 2019-02-26T23:29:28.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2023-01-05T05:51:56.000Z (about 3 years ago)
- Last Synced: 2025-06-10T10:16:48.371Z (10 months ago)
- Topics: api-wrapper, bracket-operator, functors, getters, index-operator, javascript, nodejs, npm-package, operator, overloading, setters, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/proxc
- Size: 380 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 17
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

[](https://travis-ci.com/rioam2/ProxC)
[](https://coveralls.io/github/rioam2/ProxC?branch=master)
[](https://github.com/ellerbrock/typescript-badges/)
[](https://github.com/rioam2/bstjs)
[](https://img.shields.io/badge/license-MIT-blue.svg)
ProxC is an NPM Package that allows you to create extensible classes and design declarative APIs that are a joy to use.
## Motivation
This package was heavily inspired by C++. In C++, you can overload the functionality of basic operators used on a class. This offers a new level of extensibility for defining custom APIs for your class implementations that are often exclusive to more lower-level programming languages.
In Javascript, Proxy Objects allow you to intercept and define custom behavior for fundamental operations such as property lookup/access `[]`, assignment `=`, enumeration, and function invocation `()`. These can be used to design extensible ES6 classes that allow synthetic operator definition with functionality similar to that of operator overloading in languages such as C++.
ProxC aims to combine the functionality of ES6 Proxy Objects with the convenience Javascript Classes. No functionality is gained over using Proxy Objects, however, ProxC allows developers to stay away from convoluted implementation details and define customizable APIs that are both easier to use and maintain.
## API Documentation
By extending ProxC in your class definition and defining the following member functions, you are able to design a fully custom interface for your API:
- **\_\_invoke\_\_(`...args: any`) → `any`**
Defines custom behavior for the class invocation/call operator, `()`. Invoked whenever a class instance is called as a function and forwards all arguments. Context is bound to the current class instance enabling you to use `this` to refer to internal class state.
If not defined, and the class attempts to be invoked, a TypeError will be thrown.
_Example: `myClass(1,2)` calls `__invoke__(1,2)` on `myClass`._
- **\_\_accessor\_\_(`key: number|string`) → `any`**
Defines custom behavior for the class 'get' operator, also known as the 'accessor' or 'index' operator (`[]` or `.`). Invoked when bracket notation or dot notation is used on a class instance and the supplied `key` is not a member of the current class implementation.
If not defined, default behavior is assumed and the class accessor operator will still work as expected.
_Example: `myClass['hello']` invokes `__accessor__('hello')` on `myClass` if and only if `myClass` does not contain a member named `hello` and `__accessor__` is implemented._
- **\_\_iterator\_\_() → `any[]`**
Defines how the class should be treated as an iterable object. Should return an array of elements that can be yielded to `for..of` loops.
If not defined, and the class attempts to be iterated, a TypeError will be thrown.
_Example: `for (const elt of myClass) {...}` loops over the iterable returned by `__iterator__` on `myClass`._
# Examples
1. [Circular Array Data Structure](examples/circularArray.js)