Created
July 3, 2024 13:47
-
-
Save RichardBray/e63f4ae1b2b2448bc4f650df967c4a24 to your computer and use it in GitHub Desktop.
New JS Decorators
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 log(originalMethod: any, _context: any) { | |
function replacementMethod(this: any, ...args: any[]) { | |
// const startTime = performance.now(); | |
console.log(`${originalMethod.name} START`); | |
const result = originalMethod.call(this, ...args); | |
console.log(`${originalMethod.name} END`); | |
// const endTime = performance.now(); | |
// console.log(`Method ${originalMethod.name} took ${endTime - startTime} milliseconds`); | |
return "result: " + result; | |
} | |
return replacementMethod; | |
} | |
class Animal { | |
#name: string; | |
constructor(name: string) { | |
this.#name = name; | |
} | |
get name() { | |
return this.#name; | |
} | |
@log | |
makeSound(): string { | |
// let sound = "Sound"; | |
console.log("running makeSound"); | |
// if (this.#name === "Dog") sound = "Bark"; | |
// return sound; | |
return "Sound"; | |
} | |
} | |
const animal = new Animal("Dog"); | |
const sound = animal.makeSound(); | |
console.log(sound); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment