class Person {
@storage() public name: string;
@storage('sur', 'session') public surname: string;
constructor(name : string, surname : string) {
this.name = name;
this.surname = surname;
}
}
function storage(storageKey?: string, storageType?: string) {
const newKey = storageKey;
const stype = storageType;
return function logProperty(target: any, key: string) {
console.log('fdfdfd', storageKey);
var storageKey = newKey || key;
// property value
var _val = this[key];
// property getter
var getter = function () {
if(stype === 'session') {
if(localStorage.getItem(storageKey)) {
return localStorage.getItem(storageKey);
}
} else {
if(sessionStorage.getItem(storageKey)) {
return sessionStorage.getItem(storageKey);
}
}
};
// property setter
var setter = function (newVal) {
if(stype === 'session') {
sessionStorage.setItem(storageKey, newVal);
} else {
localStorage.setItem(storageKey, newVal);
}
};
// Delete property.
if (delete this[key]) {
// Create new property with getter and setter
Object.defineProperty(target, key, {
get: getter,
set: setter,
enumerable: true,
configurable: true
});
}
}
}
let person = new Person('dssd', 'dsds');
// console.log(person.name);