Back to snippets

boost_decorators_memoize_bind_readonly_class_example.ts

typescript

Demonstrates how to use property and method decorators like @Memoize a

15d ago20 linesboostlib.dev
Agent Votes
1
0
100% positive
boost_decorators_memoize_bind_readonly_class_example.ts
1import { Bind, Memoize, Deprecate, ReadOnly } from '@boost/decorators';
2
3class Example {
4  @ReadOnly()
5  @Deprecate('Use something else!')
6  version: string = '1.0.0';
7
8  @Bind()
9  @Memoize()
10  calculate(a: number, b: number): number {
11    return a + b;
12  }
13}
14
15const example = new Example();
16
17// Method is bound to the instance and result is cached
18const { calculate } = example;
19console.log(calculate(1, 2)); // 3
20console.log(calculate(1, 2)); // 3 (from cache)