GithubHelp home page GithubHelp logo

tooppoo / relegater Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 14 KB

provide function which delegates a property access or method call to other object.

License: MIT License

JavaScript 26.47% TypeScript 73.53%

relegater's Introduction

relegater

Build Status Coverage Status

provide functions in order to delegate a property access or method call for base object to other.

Interfaces of the functions imitate delegate method in Ruby on Rails.

example

import { delegate, $delegate } from 'relegater';

const baseObj = {
  a: 1, b: 'b'
};
const delegated = {
  c: () => 'c',
  d: {
    e: true
  }
};

// delegate() create new object.
// no update to base object exist.
var result = delegate(baseObj).to(delegated, 'c', 'd').self;

result.a // 1
result.b // 'b'
result.c() // 'c'
result.d.e // true

baseObj.a // 1
baseObj.b // 'b'
baseObj.c // undefined
baseObj.d // undefined

// $delegate() modify base object destructively.
$delegate(baseObj).to(delegated, 'c', 'd');

baseObj.a // 1
baseObj.b // 'b'
baseObj.c() // 'c'
baseObj.d.e // true

baseObj is a object which receive actually property accesses or method calls.

when you want to delegate reference for baseObj to other, you should pass baseObj to delegate() or $delegate().

delegate()

delegate() create a new object which extends baseObj.

this function don't modify baseObj, so you use the created object instead of baseObj.

you can get the object from delegate({}).self.

$delegate()

$delegate() modify baseObj directly and destructively, but not modify prototype of baseObj.

how to delegate

delegate()/$delegate() return object which has following property.

  • to(delegated, prop1, prop2, ... propX)

    this method return a new object which has same interface. this method can chain. when a property is specified twice in the method chain, later is preferred.

    delegated is a object which baseObj delegate some property accesses or method calls to.

    • delegated must be object. if this is not object, runtime error raise.

    • prop1~X is variable arguments. each prop is string which presents property name in delegated;

      if propX does not exist in delegated, it is ignored.

  • self this property exist only when you use delegate().

    this object base on baseObj and has all properties which are specified by to().

License

MIT License

relegater's People

Contributors

tooppoo avatar

Watchers

 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.