Converting Functions to Arrow functions [duplicate]
This question already has an answer here:
What does “this” refer to in arrow functions in ES6?
5 answers
I'm learning ES6, I just want to convert my ES5 knowledge to ES6.
here's my ES5 code:
function click() {
this.className += ' grab';
setTimeout(() => (this.className = 'remove'), 0);
};
and here's my ES6 code:
const click = () => {
this.className += ' grab';
setTimeout(() => (this.className = 'remove'), 0);
console.log('RENDERING');
}
My problem is this.className += ' grab'; and setTimeout(() => (this.className = 'remove'), 0); didn't run the function. But console.log shows on the log.
Is this method don't work on arrow functions?
javascript function arrow-functions
New contributor
code for money is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
marked as duplicate by adiga, Community♦ 21 hours ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
What does “this” refer to in arrow functions in ES6?
5 answers
I'm learning ES6, I just want to convert my ES5 knowledge to ES6.
here's my ES5 code:
function click() {
this.className += ' grab';
setTimeout(() => (this.className = 'remove'), 0);
};
and here's my ES6 code:
const click = () => {
this.className += ' grab';
setTimeout(() => (this.className = 'remove'), 0);
console.log('RENDERING');
}
My problem is this.className += ' grab'; and setTimeout(() => (this.className = 'remove'), 0); didn't run the function. But console.log shows on the log.
Is this method don't work on arrow functions?
javascript function arrow-functions
New contributor
code for money is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
marked as duplicate by adiga, Community♦ 21 hours ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
thisis not a method, and is different inside an arrow function - read documentation to understand the difference ...didn't run the functionyes, it did, you just don't know what you're doing yet
– Jaromanda X
yesterday
2
thiskeyword functions differently in arrow functions. Read this section of the documentation.
– Yong Quan
yesterday
3
Aside - Consider usingel.classList.add('grab')(andel.classList.remove('grab')) instead of manipulating the string of class names manually. more info
– James
yesterday
1
This shows that not all functions should be converted to arrow functions just because arrow functions are cool :p arrow functions serve a specific purpose and should only be used as appropriate
– Jaromanda X
yesterday
add a comment |
This question already has an answer here:
What does “this” refer to in arrow functions in ES6?
5 answers
I'm learning ES6, I just want to convert my ES5 knowledge to ES6.
here's my ES5 code:
function click() {
this.className += ' grab';
setTimeout(() => (this.className = 'remove'), 0);
};
and here's my ES6 code:
const click = () => {
this.className += ' grab';
setTimeout(() => (this.className = 'remove'), 0);
console.log('RENDERING');
}
My problem is this.className += ' grab'; and setTimeout(() => (this.className = 'remove'), 0); didn't run the function. But console.log shows on the log.
Is this method don't work on arrow functions?
javascript function arrow-functions
New contributor
code for money is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
This question already has an answer here:
What does “this” refer to in arrow functions in ES6?
5 answers
I'm learning ES6, I just want to convert my ES5 knowledge to ES6.
here's my ES5 code:
function click() {
this.className += ' grab';
setTimeout(() => (this.className = 'remove'), 0);
};
and here's my ES6 code:
const click = () => {
this.className += ' grab';
setTimeout(() => (this.className = 'remove'), 0);
console.log('RENDERING');
}
My problem is this.className += ' grab'; and setTimeout(() => (this.className = 'remove'), 0); didn't run the function. But console.log shows on the log.
Is this method don't work on arrow functions?
This question already has an answer here:
What does “this” refer to in arrow functions in ES6?
5 answers
javascript function arrow-functions
javascript function arrow-functions
New contributor
code for money is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
code for money is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
code for money is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked yesterday
code for moneycode for money
363
363
New contributor
code for money is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
code for money is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
code for money is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
marked as duplicate by adiga, Community♦ 21 hours ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by adiga, Community♦ 21 hours ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
thisis not a method, and is different inside an arrow function - read documentation to understand the difference ...didn't run the functionyes, it did, you just don't know what you're doing yet
– Jaromanda X
yesterday
2
thiskeyword functions differently in arrow functions. Read this section of the documentation.
– Yong Quan
yesterday
3
Aside - Consider usingel.classList.add('grab')(andel.classList.remove('grab')) instead of manipulating the string of class names manually. more info
– James
yesterday
1
This shows that not all functions should be converted to arrow functions just because arrow functions are cool :p arrow functions serve a specific purpose and should only be used as appropriate
– Jaromanda X
yesterday
add a comment |
1
thisis not a method, and is different inside an arrow function - read documentation to understand the difference ...didn't run the functionyes, it did, you just don't know what you're doing yet
– Jaromanda X
yesterday
2
thiskeyword functions differently in arrow functions. Read this section of the documentation.
– Yong Quan
yesterday
3
Aside - Consider usingel.classList.add('grab')(andel.classList.remove('grab')) instead of manipulating the string of class names manually. more info
– James
yesterday
1
This shows that not all functions should be converted to arrow functions just because arrow functions are cool :p arrow functions serve a specific purpose and should only be used as appropriate
– Jaromanda X
yesterday
1
1
this is not a method, and is different inside an arrow function - read documentation to understand the difference ... didn't run the function yes, it did, you just don't know what you're doing yet– Jaromanda X
yesterday
this is not a method, and is different inside an arrow function - read documentation to understand the difference ... didn't run the function yes, it did, you just don't know what you're doing yet– Jaromanda X
yesterday
2
2
this keyword functions differently in arrow functions. Read this section of the documentation.– Yong Quan
yesterday
this keyword functions differently in arrow functions. Read this section of the documentation.– Yong Quan
yesterday
3
3
Aside - Consider using
el.classList.add('grab') (and el.classList.remove('grab')) instead of manipulating the string of class names manually. more info– James
yesterday
Aside - Consider using
el.classList.add('grab') (and el.classList.remove('grab')) instead of manipulating the string of class names manually. more info– James
yesterday
1
1
This shows that not all functions should be converted to arrow functions just because arrow functions are cool :p arrow functions serve a specific purpose and should only be used as appropriate
– Jaromanda X
yesterday
This shows that not all functions should be converted to arrow functions just because arrow functions are cool :p arrow functions serve a specific purpose and should only be used as appropriate
– Jaromanda X
yesterday
add a comment |
6 Answers
6
active
oldest
votes
There's not really enough context to give you a good answer, but one thing stands out. Arrow functions maintain scope, so this inside function click() and const click may well be different. In the ES6 version, this will refer to whatever was this during the closure creation, which may not be what you want.
Arrow Functions at MDN clears it up:
An arrow function does not have its own
this.
…Which means that this will be inherited from the declaring scope.
ES6 arrow functions aren't just a new way of declaring functions, and there's nothing inherently wrong with function myFunction(...) syntax, nor is it going away. Arrow functions avoid some verbosity when passing a function as an argument (e.g. to forEach) and avoid the need to rebind a function to a different this in some cases. Converting all function declarations to arrow syntax is not an upgrade.
@jaromanda-x Yeah, you're probably right, especially if you assume that the function is normally called as an object method, which is why it would even have a className property. Shrug.
– adc
yesterday
add a comment |
The reason is that you just need to slightly restructure things.
setTimeout(() => {this.className = 'remove'}, 0)
You have parenthesis vs curly braces.
your this may or may not work depending on how things are structured in the other code
absolutely no difference in this context - in other context, the difference is what the arrow function returns
– Jaromanda X
yesterday
add a comment |
In Arrow Functions, this isn't the this you would expect. this in Arrow Functions is defined when you create the function - not when it is called. See here for more information on that.
Thanks to @Jaromanda X from the comments - In this case, keep using standard function notation (function() {...}) - i.e. just because you bought a new screwdriver, doesn't mean the old hammer isn't still the best tool for banging in nails
1
thisdoes exist, otherwise the OP would get errors -thisis from the bounding lexical scope
– Jaromanda X
yesterday
@JaromandaX you're right. let me fix that
– Aniket G
yesterday
keep using standard function notation- i.e. just because you bought a new screwdriver, doesn't mean the old hammer isn't still the best tool for banging in nails
– Jaromanda X
yesterday
@JaromandaX that's actually a very good analogy. Do you mind if I put that in my answer?
– Aniket G
yesterday
Feel free - it's one of my better ones :p
– Jaromanda X
yesterday
add a comment |
const click = () => {
console.log(this);
this.className += ' grab';
setTimeout(() => (this.className = 'remove'), 0);
console.log('RENDERING');
}
click();
'this' in the arrow function represents from wherever it is called. for eg if i open the browser and goto console and type above code then 'this' will become window object since the function is called from global enviroment. Also arrow function doesnot have its own 'this'.
add a comment |
You can bind this for arrow function to access functions and data. Your code should be something like
const click = () => {
this.className += ' grab';
setTimeout(() => (this.className = 'remove'), 0);
console.log('RENDERING');
}.bind(this)
It will bind this for arrow function and you can access those variable and functions.
it will bindthisonly if it's defined in the object though
– somebody
23 hours ago
add a comment |
An arrow function expression is a syntactically compact alternative to a regular function expression, although without its own bindings to the this, arguments, super, or new.target keywords. Arrow function expressions are ill suited as methods, and they cannot be used as constructors.
add a comment |
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
There's not really enough context to give you a good answer, but one thing stands out. Arrow functions maintain scope, so this inside function click() and const click may well be different. In the ES6 version, this will refer to whatever was this during the closure creation, which may not be what you want.
Arrow Functions at MDN clears it up:
An arrow function does not have its own
this.
…Which means that this will be inherited from the declaring scope.
ES6 arrow functions aren't just a new way of declaring functions, and there's nothing inherently wrong with function myFunction(...) syntax, nor is it going away. Arrow functions avoid some verbosity when passing a function as an argument (e.g. to forEach) and avoid the need to rebind a function to a different this in some cases. Converting all function declarations to arrow syntax is not an upgrade.
@jaromanda-x Yeah, you're probably right, especially if you assume that the function is normally called as an object method, which is why it would even have a className property. Shrug.
– adc
yesterday
add a comment |
There's not really enough context to give you a good answer, but one thing stands out. Arrow functions maintain scope, so this inside function click() and const click may well be different. In the ES6 version, this will refer to whatever was this during the closure creation, which may not be what you want.
Arrow Functions at MDN clears it up:
An arrow function does not have its own
this.
…Which means that this will be inherited from the declaring scope.
ES6 arrow functions aren't just a new way of declaring functions, and there's nothing inherently wrong with function myFunction(...) syntax, nor is it going away. Arrow functions avoid some verbosity when passing a function as an argument (e.g. to forEach) and avoid the need to rebind a function to a different this in some cases. Converting all function declarations to arrow syntax is not an upgrade.
@jaromanda-x Yeah, you're probably right, especially if you assume that the function is normally called as an object method, which is why it would even have a className property. Shrug.
– adc
yesterday
add a comment |
There's not really enough context to give you a good answer, but one thing stands out. Arrow functions maintain scope, so this inside function click() and const click may well be different. In the ES6 version, this will refer to whatever was this during the closure creation, which may not be what you want.
Arrow Functions at MDN clears it up:
An arrow function does not have its own
this.
…Which means that this will be inherited from the declaring scope.
ES6 arrow functions aren't just a new way of declaring functions, and there's nothing inherently wrong with function myFunction(...) syntax, nor is it going away. Arrow functions avoid some verbosity when passing a function as an argument (e.g. to forEach) and avoid the need to rebind a function to a different this in some cases. Converting all function declarations to arrow syntax is not an upgrade.
There's not really enough context to give you a good answer, but one thing stands out. Arrow functions maintain scope, so this inside function click() and const click may well be different. In the ES6 version, this will refer to whatever was this during the closure creation, which may not be what you want.
Arrow Functions at MDN clears it up:
An arrow function does not have its own
this.
…Which means that this will be inherited from the declaring scope.
ES6 arrow functions aren't just a new way of declaring functions, and there's nothing inherently wrong with function myFunction(...) syntax, nor is it going away. Arrow functions avoid some verbosity when passing a function as an argument (e.g. to forEach) and avoid the need to rebind a function to a different this in some cases. Converting all function declarations to arrow syntax is not an upgrade.
edited yesterday
answered yesterday
adcadc
32127
32127
@jaromanda-x Yeah, you're probably right, especially if you assume that the function is normally called as an object method, which is why it would even have a className property. Shrug.
– adc
yesterday
add a comment |
@jaromanda-x Yeah, you're probably right, especially if you assume that the function is normally called as an object method, which is why it would even have a className property. Shrug.
– adc
yesterday
@jaromanda-x Yeah, you're probably right, especially if you assume that the function is normally called as an object method, which is why it would even have a className property. Shrug.
– adc
yesterday
@jaromanda-x Yeah, you're probably right, especially if you assume that the function is normally called as an object method, which is why it would even have a className property. Shrug.
– adc
yesterday
add a comment |
The reason is that you just need to slightly restructure things.
setTimeout(() => {this.className = 'remove'}, 0)
You have parenthesis vs curly braces.
your this may or may not work depending on how things are structured in the other code
absolutely no difference in this context - in other context, the difference is what the arrow function returns
– Jaromanda X
yesterday
add a comment |
The reason is that you just need to slightly restructure things.
setTimeout(() => {this.className = 'remove'}, 0)
You have parenthesis vs curly braces.
your this may or may not work depending on how things are structured in the other code
absolutely no difference in this context - in other context, the difference is what the arrow function returns
– Jaromanda X
yesterday
add a comment |
The reason is that you just need to slightly restructure things.
setTimeout(() => {this.className = 'remove'}, 0)
You have parenthesis vs curly braces.
your this may or may not work depending on how things are structured in the other code
The reason is that you just need to slightly restructure things.
setTimeout(() => {this.className = 'remove'}, 0)
You have parenthesis vs curly braces.
your this may or may not work depending on how things are structured in the other code
edited yesterday
answered yesterday
Jon BlackJon Black
860718
860718
absolutely no difference in this context - in other context, the difference is what the arrow function returns
– Jaromanda X
yesterday
add a comment |
absolutely no difference in this context - in other context, the difference is what the arrow function returns
– Jaromanda X
yesterday
absolutely no difference in this context - in other context, the difference is what the arrow function returns
– Jaromanda X
yesterday
absolutely no difference in this context - in other context, the difference is what the arrow function returns
– Jaromanda X
yesterday
add a comment |
In Arrow Functions, this isn't the this you would expect. this in Arrow Functions is defined when you create the function - not when it is called. See here for more information on that.
Thanks to @Jaromanda X from the comments - In this case, keep using standard function notation (function() {...}) - i.e. just because you bought a new screwdriver, doesn't mean the old hammer isn't still the best tool for banging in nails
1
thisdoes exist, otherwise the OP would get errors -thisis from the bounding lexical scope
– Jaromanda X
yesterday
@JaromandaX you're right. let me fix that
– Aniket G
yesterday
keep using standard function notation- i.e. just because you bought a new screwdriver, doesn't mean the old hammer isn't still the best tool for banging in nails
– Jaromanda X
yesterday
@JaromandaX that's actually a very good analogy. Do you mind if I put that in my answer?
– Aniket G
yesterday
Feel free - it's one of my better ones :p
– Jaromanda X
yesterday
add a comment |
In Arrow Functions, this isn't the this you would expect. this in Arrow Functions is defined when you create the function - not when it is called. See here for more information on that.
Thanks to @Jaromanda X from the comments - In this case, keep using standard function notation (function() {...}) - i.e. just because you bought a new screwdriver, doesn't mean the old hammer isn't still the best tool for banging in nails
1
thisdoes exist, otherwise the OP would get errors -thisis from the bounding lexical scope
– Jaromanda X
yesterday
@JaromandaX you're right. let me fix that
– Aniket G
yesterday
keep using standard function notation- i.e. just because you bought a new screwdriver, doesn't mean the old hammer isn't still the best tool for banging in nails
– Jaromanda X
yesterday
@JaromandaX that's actually a very good analogy. Do you mind if I put that in my answer?
– Aniket G
yesterday
Feel free - it's one of my better ones :p
– Jaromanda X
yesterday
add a comment |
In Arrow Functions, this isn't the this you would expect. this in Arrow Functions is defined when you create the function - not when it is called. See here for more information on that.
Thanks to @Jaromanda X from the comments - In this case, keep using standard function notation (function() {...}) - i.e. just because you bought a new screwdriver, doesn't mean the old hammer isn't still the best tool for banging in nails
In Arrow Functions, this isn't the this you would expect. this in Arrow Functions is defined when you create the function - not when it is called. See here for more information on that.
Thanks to @Jaromanda X from the comments - In this case, keep using standard function notation (function() {...}) - i.e. just because you bought a new screwdriver, doesn't mean the old hammer isn't still the best tool for banging in nails
edited yesterday
answered yesterday
Aniket GAniket G
2,237326
2,237326
1
thisdoes exist, otherwise the OP would get errors -thisis from the bounding lexical scope
– Jaromanda X
yesterday
@JaromandaX you're right. let me fix that
– Aniket G
yesterday
keep using standard function notation- i.e. just because you bought a new screwdriver, doesn't mean the old hammer isn't still the best tool for banging in nails
– Jaromanda X
yesterday
@JaromandaX that's actually a very good analogy. Do you mind if I put that in my answer?
– Aniket G
yesterday
Feel free - it's one of my better ones :p
– Jaromanda X
yesterday
add a comment |
1
thisdoes exist, otherwise the OP would get errors -thisis from the bounding lexical scope
– Jaromanda X
yesterday
@JaromandaX you're right. let me fix that
– Aniket G
yesterday
keep using standard function notation- i.e. just because you bought a new screwdriver, doesn't mean the old hammer isn't still the best tool for banging in nails
– Jaromanda X
yesterday
@JaromandaX that's actually a very good analogy. Do you mind if I put that in my answer?
– Aniket G
yesterday
Feel free - it's one of my better ones :p
– Jaromanda X
yesterday
1
1
this does exist, otherwise the OP would get errors - this is from the bounding lexical scope– Jaromanda X
yesterday
this does exist, otherwise the OP would get errors - this is from the bounding lexical scope– Jaromanda X
yesterday
@JaromandaX you're right. let me fix that
– Aniket G
yesterday
@JaromandaX you're right. let me fix that
– Aniket G
yesterday
keep using standard function notation - i.e. just because you bought a new screwdriver, doesn't mean the old hammer isn't still the best tool for banging in nails– Jaromanda X
yesterday
keep using standard function notation - i.e. just because you bought a new screwdriver, doesn't mean the old hammer isn't still the best tool for banging in nails– Jaromanda X
yesterday
@JaromandaX that's actually a very good analogy. Do you mind if I put that in my answer?
– Aniket G
yesterday
@JaromandaX that's actually a very good analogy. Do you mind if I put that in my answer?
– Aniket G
yesterday
Feel free - it's one of my better ones :p
– Jaromanda X
yesterday
Feel free - it's one of my better ones :p
– Jaromanda X
yesterday
add a comment |
const click = () => {
console.log(this);
this.className += ' grab';
setTimeout(() => (this.className = 'remove'), 0);
console.log('RENDERING');
}
click();
'this' in the arrow function represents from wherever it is called. for eg if i open the browser and goto console and type above code then 'this' will become window object since the function is called from global enviroment. Also arrow function doesnot have its own 'this'.
add a comment |
const click = () => {
console.log(this);
this.className += ' grab';
setTimeout(() => (this.className = 'remove'), 0);
console.log('RENDERING');
}
click();
'this' in the arrow function represents from wherever it is called. for eg if i open the browser and goto console and type above code then 'this' will become window object since the function is called from global enviroment. Also arrow function doesnot have its own 'this'.
add a comment |
const click = () => {
console.log(this);
this.className += ' grab';
setTimeout(() => (this.className = 'remove'), 0);
console.log('RENDERING');
}
click();
'this' in the arrow function represents from wherever it is called. for eg if i open the browser and goto console and type above code then 'this' will become window object since the function is called from global enviroment. Also arrow function doesnot have its own 'this'.
const click = () => {
console.log(this);
this.className += ' grab';
setTimeout(() => (this.className = 'remove'), 0);
console.log('RENDERING');
}
click();
'this' in the arrow function represents from wherever it is called. for eg if i open the browser and goto console and type above code then 'this' will become window object since the function is called from global enviroment. Also arrow function doesnot have its own 'this'.
answered yesterday
Kaushal RegmiKaushal Regmi
538
538
add a comment |
add a comment |
You can bind this for arrow function to access functions and data. Your code should be something like
const click = () => {
this.className += ' grab';
setTimeout(() => (this.className = 'remove'), 0);
console.log('RENDERING');
}.bind(this)
It will bind this for arrow function and you can access those variable and functions.
it will bindthisonly if it's defined in the object though
– somebody
23 hours ago
add a comment |
You can bind this for arrow function to access functions and data. Your code should be something like
const click = () => {
this.className += ' grab';
setTimeout(() => (this.className = 'remove'), 0);
console.log('RENDERING');
}.bind(this)
It will bind this for arrow function and you can access those variable and functions.
it will bindthisonly if it's defined in the object though
– somebody
23 hours ago
add a comment |
You can bind this for arrow function to access functions and data. Your code should be something like
const click = () => {
this.className += ' grab';
setTimeout(() => (this.className = 'remove'), 0);
console.log('RENDERING');
}.bind(this)
It will bind this for arrow function and you can access those variable and functions.
You can bind this for arrow function to access functions and data. Your code should be something like
const click = () => {
this.className += ' grab';
setTimeout(() => (this.className = 'remove'), 0);
console.log('RENDERING');
}.bind(this)
It will bind this for arrow function and you can access those variable and functions.
answered yesterday
ZearaeZZearaeZ
710418
710418
it will bindthisonly if it's defined in the object though
– somebody
23 hours ago
add a comment |
it will bindthisonly if it's defined in the object though
– somebody
23 hours ago
it will bind
this only if it's defined in the object though– somebody
23 hours ago
it will bind
this only if it's defined in the object though– somebody
23 hours ago
add a comment |
An arrow function expression is a syntactically compact alternative to a regular function expression, although without its own bindings to the this, arguments, super, or new.target keywords. Arrow function expressions are ill suited as methods, and they cannot be used as constructors.
add a comment |
An arrow function expression is a syntactically compact alternative to a regular function expression, although without its own bindings to the this, arguments, super, or new.target keywords. Arrow function expressions are ill suited as methods, and they cannot be used as constructors.
add a comment |
An arrow function expression is a syntactically compact alternative to a regular function expression, although without its own bindings to the this, arguments, super, or new.target keywords. Arrow function expressions are ill suited as methods, and they cannot be used as constructors.
An arrow function expression is a syntactically compact alternative to a regular function expression, although without its own bindings to the this, arguments, super, or new.target keywords. Arrow function expressions are ill suited as methods, and they cannot be used as constructors.
answered yesterday
Bathri NathanBathri Nathan
937
937
add a comment |
add a comment |
1
thisis not a method, and is different inside an arrow function - read documentation to understand the difference ...didn't run the functionyes, it did, you just don't know what you're doing yet– Jaromanda X
yesterday
2
thiskeyword functions differently in arrow functions. Read this section of the documentation.– Yong Quan
yesterday
3
Aside - Consider using
el.classList.add('grab')(andel.classList.remove('grab')) instead of manipulating the string of class names manually. more info– James
yesterday
1
This shows that not all functions should be converted to arrow functions just because arrow functions are cool :p arrow functions serve a specific purpose and should only be used as appropriate
– Jaromanda X
yesterday