Last active
February 12, 2020 13:21
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
function logProperty(target: any, key: string) { | |
const newKey = `_${key}`; | |
Object.defineProperty(target, key, { | |
get() { | |
console.log(`Get: ${key} => ${this[newKey]}`); | |
return this[newKey]; | |
}, | |
set(newVal) { | |
console.log(`Set: ${key} => ${newVal}`); | |
this[newKey] = newVal; | |
}, | |
enumerable: true, | |
configurable: true | |
}); | |
} | |
class Task { | |
@logProperty | |
public id: number; | |
@logProperty | |
public title: string; | |
constructor(id : number, title : string) { | |
this.id = id; | |
this.title = title; | |
} | |
} | |
const task = new Task(1, 'Write more articles'); | |
// Set: id => 1 | |
// Set: title => Write more articles | |
console.log(task.id, task.title); | |
// Get: id => 1 | |
// Get: title => Write more articles | |
// 1 'Write more articles' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment