I am wondering what is difference between Name(){ } and function NAME() { } and Name = () => { }?
When I used Name(){ }, I got an error and then I modified it to Name = () => { } after that it worked.
//this doesn't work.
onClickHandler(){
let nextVersion = parseInt(this.state.version, 10) + 1
this.setState({ version: nextVersion.toFixed(1)})
}
//this works.
onClickHandler = () => {
let nextVersion = parseInt(this.state.version, 10) + 1
this.setState({ version: nextVersion.toFixed(1)})
}
3 Answers 3
The first example is not valid syntax. It doesn't define or invoke anything and will result in an error because the JavaScript runtime will think that you want to invoke the onClickHandler function, but then won't understand what the following block (delimited by {} is for):
onClickHandler(){
let nextVersion = parseInt(this.state.version, 10) + 1
this.setState({ version: nextVersion.toFixed(1)})
}
The second is a variable assignment of an Arrow Function to the variable and while the syntax is valid, the use of this within an Arrow Function will not produce the same results as with a function declaration or function expression:
let onClickHandler = () => {
let nextVersion = parseInt(this.state.version, 10) + 1
this.setState({ version: nextVersion.toFixed(1)})
}
The following is a Function Declaration. Function Declarations are hoisted, can be passed as data, and they can be invoked as functions or constructor functions:
function onClickHandler() {
let nextVersion = parseInt(this.state.version, 10) + 1
this.setState({ version: nextVersion.toFixed(1)})
}
The following is a Function Expression (an anonymous function assigned to another item). The function portion of the expression is not hoisted.:
let onClickHandler = function () {
let nextVersion = parseInt(this.state.version, 10) + 1
this.setState({ version: nextVersion.toFixed(1)})
}
6 Comments
onClickHandler(){} is valid syntax for defining ES6 class methods.I am wondering what is difference between Name(){ } and function NAME() { } and Name = () => { }. and saying that his first example was invalid javascript syntax is only partially true. Given the way he was using it, yes. But why not explain that he is using it wrong rather than simply saying it is invalid syntax, period. Now other people will see your answer and assume that it is invalid syntax always. Then be confused when they see it actually being used.Just for the sake of clarification, I'll add in my two cents.
Your first example
onClickHandler(){
let nextVersion = parseInt(this.state.version, 10) + 1
this.setState({ version: nextVersion.toFixed(1)})
}
is valid syntax for defining ES6 class methods. Heres an example:
class MyAwesomeClass {
constructor() {
this.name="Isaac"
}
printName() {
console.log(this.name)
}
}
let m = new MyAwesomeClass();
m.printName();
Honestly don't know why so many people were quick to say it was "invalid JavaScript syntax", but wouldn't explain that it is acceptable syntax in some situations.
3 Comments
TypeError: Cannot read property 'state' of undefinedthis.setState im assuming you have a react app. Depending on where you are calling the function, the context of this will change. You may need to say this.onClickHandler = this.onClickHandler.bind(this) in the constructor.I think this might be what you are potentially talking about. Let's say we have a click event:
Given this html:
<button id="button">click me</button>
You might handle the click event like this:
document.getElementById('button').addEventListener('click', onClickHandler)
Let's say you used the function keyword in your first example and it looked like this:
function onClickHandler(){
let nextVersion = parseInt(this.state.version, 10) + 1
this.setState({ version: nextVersion.toFixed(1)})
}
In this example this is actually the dom element you're clicking on.
In your second example:
const onClickHandler = () => {
let nextVersion = parseInt(this.state.version, 10) + 1
this.setState({ version: nextVersion.toFixed(1)})
}
The handler leverages an arrow function which has lexical bound this. This means that the value of this or context is bound to the originating context. Which since you said "this works" means it's what you want it to be, not the button(aka. dom element).
thiswon't refer to what you expect it to.thisin relation to the functionclassdefinition or an object literal. But on its own it is a syntax error. It's like saying that sometimes-works and sometimes not. It works in an expression, not as a stand-alone statement.