JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://amatiasq.com/fiddle-console.js"></script>
JavaScript
const timer = (target, name, descriptor) => {
// create a reference to the function
let fn = descriptor.value
// modify!
descriptor.value = function () {
console.log('comienza ' + name)
let result = fn.apply(this, arguments)
console.log('termina ' + name)
}
// apply on property
//Object.defineProperty(target, name, descriptor);
}
class Person {
constructor (first = '', last = '') {
this.first = first
this.last = last
this.age = 0
}
greeting () {
return `Hi! I'm ${this.first} ${this.last}. Nice to meet you!`
}
@timer
grow (age) {
for (let i = 0; i < age; i++) {
this.age = i
}
}
}
const p = new Person('Edgar Bermejo')
console.log(p.greeting()); // Hi! I'm Edgar Bermejo . Nice to meet you!
p.grow(9)