lopatnov/callable


A callable object is a data structure that behaves as both an object and a function.

https://lopatnov.github.io/callable/

License: Apache-2.0

Language: JavaScript

Keywords: callable, function, javascript, javascript-library, object, typescript, typescript-library


@lopatnov/callable

NPM version License Build Status Twitter

A callable object is a data structure that behaves as both an object and a function.

Install

https://nodei.co/npm/@lopatnov/callable.png?downloads=true&downloadRank=true&stars=true

npm install @lopatnov/callable

Browser

<script src="//lopatnov.github.io/callable/dist/callable.min.js"></script>

Import package to the project

import {
  Callable,
  CallableByCallee,
  CallableByClosure,
  CallableByProxy
} from "callable";

or

var module = require("callable");
var Callable = module.Callable,
  CallableByCallee = module.CallableByCallee,
  CallableByClosure = module.CallableByClosure,
  CallableByProxy = module.CallableByProxy;

How to use

class ChildCallable extends Callable<string> {
      _call(...args: any[]): string {
        return `Hello ${args[0]}`;
      }
    }

let x = new ChildCallable();
let xc = x("World");

console.log(xc); //Hello World

abstract class Callable<TResult>

The Bind Way

Pros

  • Doesn’t rely on deprecated or modern features.
  • No need to modify prototypes.

Cons

  • Requires wrapping the function object in a bound function.

abstract class CallableByCallee<TResult>

Pros

  • Very simple.
  • No need to modify prototypes.

Cons

  • arguments and arguments.callee are unavailable in ‘strict mode’, see MDN for more.

abstract class CallableByProxy<TResult>

Pros

  • Simple, native way to intercept calls and redirect them.
  • No need to modify prototypes.

Cons

  • Requires wrapping objects created by Callable in a Proxy.
  • A small performance penalty for using Proxy handlers.

abstract class CallableByClosure<TResult>

Pros

  • Requires no wrapping of the returned object with a Proxy or bind.

Cons

  • Requires modifying prototypes.
  • Modifying prototypes is slow and has other side effects, see MDN.

Demo

See, how it's working: https://runkit.com/lopatnov/callable-demo

Test it with a runkit: https://npm.runkit.com/@lopatnov/callable

Rights and Agreements

License Apache-2.0

Copyright 2019 Oleksandr Lopatnov

Project Statistics

Sourcerank 3
Repository Size 69.3 KB
Stars 0
Forks 0
Watchers 1
Open issues 0
Dependencies 447
Contributors 1
Tags 2
Created
Last updated
Last pushed

Top Contributors See all

Oleksandr

Packages Referencing this Repo

@lopatnov/callable
Callable abstraction for making function-like objects
Latest release 1.0.1 - Updated

Recent Tags See all

1.0.1 December 29, 2019
1.0.0 December 28, 2019

Something wrong with this page? Make a suggestion

Last synced: 2019-12-28 01:51:35 UTC

Login to resync this repository