I'm learning JavaScript/Node and I found out a way to create functions from an array in ES5 but not in ES6.
ES5
const methods = ['info', 'warm', 'debug', 'error', 'fatal']
function Logger(name){
this.name = name;
return this
}
methods.forEach((method) => {
Logger.prototype[method] = function (msg) {
tags = {}
tags.id = uuidv1()
tags.level = method.toUpperCase()
tags.event = msg
tags.timestamp = Date.now()
console.log(tags)
}
})
So when I call
log.fatal('fatal error')
Prints the correct way I want to do little bit the same but in ES6 using Classes
So far I have ES6
class Logger {
constructor(name) {
this._name = name;
}
get name() {
return this._name
}
set name(name) {
this._name = name
}
//I tried with iterator but it wasn't correct.
* [methods]()
methods.forEach(method =>{
[method] = function (msg) {
tags = {}
tags.id = uuidv1()
tags.level = method.toUpperCase()
tags.event = msg
tags.timestamp = Date.now()
console.log(json2tags(tags))
}
})
}
Thank you!