decorate-object
TypeScript icon, indicating that this package has built-in type declarations

0.0.2 • Public • Published

Helps decorate JavaScript objects using the Decorator Pattern.

Motivation

The decorator pattern works well for classes, when you know the exact interface and exactly which methods you want to decorate.

However, there are cases in which it helps to decorate an object during runtime.

Installation

npm install --save decorate-object

Usage

You can use decorate to decorate any object. The decorator is defined using a decorator factory function.

The decorator factory is used to create a decorator. The decorator can (but doesn't have to) define any method that is part of the delegate object.

When a method of the decorated object is called, first, said method is called on the delegate object. The return value of this function call is then handed over to the decorator factory as originalValue to create a new decorator object.

Then, the method is called on the decorator object if defined by the decorator. The decorator object has access to both the method's parameters and to the originalValue. The return value of the decorator's method is then returned to the client.

Example

See the tests

Readme

Keywords

Package Sidebar

Install

npm i decorate-object

Weekly Downloads

2

Version

0.0.2

License

BSD-2-Clause

Unpacked Size

16.3 kB

Total Files

23

Last publish

Collaborators

  • fchristl