Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
149
rated 0 times [  154] [ 5]  / answers: 1 / hits: 39286  / 8 Years ago, fri, october 28, 2016, 12:00:00

I wanted to update my javascript code to the new ES6 Standard, so I looked at how functions are now written and tried it out on a global function of mine, which reads like this in the old es5



function logMessage(message) {
document.getElementById(logs).innerHTML = document.getElementById(logs).innerHTML + `<li class=item-padding> ${message} </li>`
}


now if I'm not wrong the correct transformation to ES6 would be like this:



logMessage = message => {
etc
}


But my ESLint tells me that my logMessage is not defined and I get an error in my console, do I miss something? Do I have to declare var, let or const before the logMessage?



I don't know if its important, but I also want to export this function from file One to file Two and use the function logMessage in another function in file Two, is there something I have to keep in mind when doing so?



Thanks for any help!



Edit: Thanks everyone the answers helped me a lot, got my problem fixed!


More From » function

 Answers
22

now if I'm not wrong the correct transformation to es6 would be like this




You're wrong.



Arrow functions are a new syntax with different behaviour. They are not a straight up replacement for function declarations and function expressions (both of which still exist in ES6).




But my ESLint tells me that my logMessage is not defined and I get an error in my console, do I miss something? Do I have to declare var, let or const before the logMessage?




Yes. You're assigning something to a variable. You must declare the variable first.




I also want to export this function from file One to file Two




How you define the function has no bearing on your ability to export it.


[#60250] Tuesday, October 25, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
iselauniquef

Total Points: 443
Total Questions: 98
Total Answers: 102

Location: Saint Vincent and the Grenadines
Member since Thu, Oct 15, 2020
4 Years ago
;