Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
lteacher committed Aug 24, 2016
1 parent 6110a48 commit cfaf763
Showing 1 changed file with 42 additions and 3 deletions.
45 changes: 42 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,45 @@
# Auto Delegate

> A simple auto delegation utility
> A simple auto delegation utility which attaches all the properties of some delegate to a single property under the owner
Provides simple functionality to attach a delegate property to an object and auto
map all of the properties over so they go through the delegate.
## What for?

This can be helpful if you want to mixin some object instance and its functionality without directly extending the target object.

Here is some random example from the tests (sort of)

```javascript
// There is some counter class (babel-ified)
class Counter {
let count = 0;

increment = () => this.count++;
decrement = () => this.count--;
}

// There is some point thingo
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}

get location = () => `(${this.x}, ${this.y})`;
}

/* Well here you can delegate to these two additional classes. Its not an amazing example
* but shows the idea. Its a bit more useful with a bigger object that also has its state
* spoiled when extending but this is just a demo */
class TrackedLocation {
constructor() {
delegate(this, new Counter(), '_counter');
delegate(this, new Point(0,2), '_point');
}
}

// And use the methods
let tracked = new TrackedLocation();
tracked.increment();
console.log(tracked.count); // 1
console.log(tracked.location); // '(0, 2)'
```

0 comments on commit cfaf763

Please sign in to comment.