From 60d5b20f5a07a366a93ba8877215f54b78a02c7b Mon Sep 17 00:00:00 2001 From: ztimson Date: Mon, 1 Feb 2021 22:19:32 -0500 Subject: [PATCH] Updated readme --- README.md | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ae8cef3..e2a1c32 100644 --- a/README.md +++ b/README.md @@ -24,8 +24,23 @@ export class MyCustomClass { ### Custom Functions You can technically store anything inside local/session storage however everything is serialized using javascript's JSON, -so any prototypes will be stripped causing you to lose any extra functions you may have defined on your class. However -if you provide a default value, +so anything extra (prototypes, functions, etc) will be lost. However if you provide a default value, it will be copied & +the data injected, giving you a workaround to accessing static properties. + +```typescript +class Person { + constructor(public first: string, public last: string) { } + fullName() { return `${this.last}, ${this.first}`; } +} + +LocalStorage.setItem('example', '{"first": "John", "last": "Smith"}'); +@LocalStorage(null) example!: Person; +console.log(example.fullName()) // ERROR: fullName function doesn't exist + +LocalStorage.setItem('example2', '{"first": "John", "last": "Smith"}'); +@LocalStorage(new Person(null, null)) example2!: Person; +console.log(example.fullName()) // Will work because we have a default object to figure out type +``` ### Impure Functions Impure functions don't use the Object's setter preventing the storage from being updated. To prevent this use a pure