Giter Site home page Giter Site logo

yangxin1994 / proxc Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rioam2/proxc

0.0 0.0 0.0 604 KB

๐Ÿ”— Unlock the extensiblity of ES6 Proxy Objects in Javascript Classes and create beautiful APIs

Home Page: https://www.npmjs.com/package/proxc

License: MIT License

TypeScript 100.00%

proxc's Introduction

ProxC Logo

Build Status Coverage Status TypeScript NPM Version License

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

proxc's People

Contributors

dependabot-preview[bot] avatar rioam2 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.