-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
42 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)' | ||
``` |