function setClassProperty(target) {
target.n = 'akara'
}
@setClassProperty
class People {
}
console.log(People.n)
function log(target, key, descriptor) {
const fn = descriptor.value
descriptor.value = function (...args) {
console.log(`calling ${key} with args: ${args}`)
return fn.apply(this, args)
}
return descriptor
}
class People {
constructor(name, age) {
this.name = name
this.age = age
}
@log
getName() {
return this.name
}
}
const p = new People('akara', 20)
console.log(p.getName())
function test(target, key, descriptor) {
const fn = descriptor.initializer
descriptor.initializer = function () {
const v = fn.apply(this)
const randomNumber = ~~(Math.random() * 1000)
return `${v}-${randomNumber}`
}
}
class People {
@test
name = 'akara'
}
const p = new People()
console.log(p.name)