Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring a function in ES6?

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!

like image 830
NakedPython Avatar asked Oct 28 '16 08:10

NakedPython


People also ask

How do I declare a function in ES6?

Let us first understand what exactly the function is and how to declare a function in ES6 using different syntaxes. function display(name) { console. log(name) } display("Geeksforgeeks"); let display = name = console. log(name); display("Geeksforgeeks");

How do you declare a function in JavaScript?

A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses (). Function names can contain letters, digits, underscores, and dollar signs (same rules as variables). The parentheses may include parameter names separated by commas: (parameter1, parameter2, ...)

How do you declare a function?

You can declare a function by providing its return value, name, and the types for its arguments. The names of the arguments are optional. A function definition counts as a function declaration.

What does () => mean in JavaScript?

It's a new feature that introduced in ES6 and is called arrow function. The left part denotes the input of a function and the right part the output of that function.


2 Answers

function logMessage(message) {
    // etc...
}

... is function declaration which is still perfectly valid in es6. You are converting your function declaration into a function expression, which in es5 would look like this...

logMessage = function(message) {
    // etc...
}

... and then into es6 ...

logMessage = message => {
    // etc
}

... so the linting problem is not introduced by es6 syntax, but rather using function expression, assigning to a variable which without var/let/const is a global variable. There is also a difference in the original function declaration would be hoisted, but the function expression form must be declared before it's called. There is also a difference in that es6 arrow functions retain the context of this from the parent scope, so worth noting they are not 100% direct 1 for 1 mappings of each other.

Short answer, yes, variables need to be declared with var/let/const in order to avoid becoming global variables, whether it's a function or not.

let logMessage = message => {
    // etc
}
like image 103
Billy Moon Avatar answered Sep 22 '22 10:09

Billy Moon


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.

like image 33
Quentin Avatar answered Sep 20 '22 10:09

Quentin