Converting Functions to Arrow functions [duplicate]What does “this” refer to in arrow functions in ES6?Is there an “exists” function for jQuery?What's the difference between a method and a function?How can I convert a string to boolean in JavaScript?var functionName = function() vs function functionName() Set a default parameter value for a JavaScript functionConvert a string to an integer in JavaScript?Convert form data to JavaScript object with jQueryWhat does the exclamation mark do before the function?ECMAScript 6 arrow function that returns an objectAre ES6 arrow functions incompatible with Angular?
Did arcade monitors have same pixel aspect ratio as TV sets?
It grows, but water kills it
Moving brute-force search to FPGA
PTIJ: Haman's bad computer
What happens if you are holding an Iron Flask with a demon inside and walk into an Antimagic Field?
Mimic lecturing on blackboard, facing audience
How do you respond to a colleague from another team when they're wrongly expecting that you'll help them?
Why "had" in "[something] we would have made had we used [something]"?
What if a revenant (monster) gains fire resistance?
Do the primes contain an infinite almost arithmetic progression?
How can I write humor as character trait?
Is there a RAID 0 Equivalent for RAM?
How to explain what's wrong with this application of the chain rule?
Can the US President recognize Israel’s sovereignty over the Golan Heights for the USA or does that need an act of Congress?
Does IPv6 have similar concept of network mask?
Why should universal income be universal?
Biological Blimps: Propulsion
Why is the "ls" command showing permissions of files in a FAT32 partition?
Creepy dinosaur pc game identification
Has any country ever had 2 former presidents in jail simultaneously?
Limits and Infinite Integration by Parts
On a tidally locked planet, would time be quantized?
Can a stoichiometric mixture of oxygen and methane exist as a liquid at standard pressure and some (low) temperature?
Pre-mixing cryogenic fuels and using only one fuel tank
Converting Functions to Arrow functions [duplicate]
What does “this” refer to in arrow functions in ES6?Is there an “exists” function for jQuery?What's the difference between a method and a function?How can I convert a string to boolean in JavaScript?var functionName = function() vs function functionName() Set a default parameter value for a JavaScript functionConvert a string to an integer in JavaScript?Convert form data to JavaScript object with jQueryWhat does the exclamation mark do before the function?ECMAScript 6 arrow function that returns an objectAre ES6 arrow functions incompatible with Angular?
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
marked as duplicate by adiga, Community♦ Mar 15 at 8:21
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
marked as duplicate by adiga, Community♦ Mar 15 at 8:21
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
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
Mar 15 at 4:12
2
this
keyword functions differently in arrow functions. Read this section of the documentation.
– Yong Quan
Mar 15 at 4:19
3
Aside - Consider usingel.classList.add('grab')
(andel.classList.remove('grab')
) instead of manipulating the string of class names manually. more info
– James
Mar 15 at 4:27
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
Mar 15 at 4:31
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
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
asked Mar 15 at 4:10
code for moneycode for money
363
363
marked as duplicate by adiga, Community♦ Mar 15 at 8:21
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♦ Mar 15 at 8:21
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
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
Mar 15 at 4:12
2
this
keyword functions differently in arrow functions. Read this section of the documentation.
– Yong Quan
Mar 15 at 4:19
3
Aside - Consider usingel.classList.add('grab')
(andel.classList.remove('grab')
) instead of manipulating the string of class names manually. more info
– James
Mar 15 at 4:27
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
Mar 15 at 4:31
add a comment |
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
Mar 15 at 4:12
2
this
keyword functions differently in arrow functions. Read this section of the documentation.
– Yong Quan
Mar 15 at 4:19
3
Aside - Consider usingel.classList.add('grab')
(andel.classList.remove('grab')
) instead of manipulating the string of class names manually. more info
– James
Mar 15 at 4:27
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
Mar 15 at 4:31
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
Mar 15 at 4:12
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
Mar 15 at 4:12
2
2
this
keyword functions differently in arrow functions. Read this section of the documentation.– Yong Quan
Mar 15 at 4:19
this
keyword functions differently in arrow functions. Read this section of the documentation.– Yong Quan
Mar 15 at 4:19
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
Mar 15 at 4:27
Aside - Consider using
el.classList.add('grab')
(and el.classList.remove('grab')
) instead of manipulating the string of class names manually. more info– James
Mar 15 at 4:27
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
Mar 15 at 4:31
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
Mar 15 at 4:31
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
Mar 15 at 4:29
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
Mar 15 at 4:27
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
this
does exist, otherwise the OP would get errors -this
is from the bounding lexical scope
– Jaromanda X
Mar 15 at 4:26
@JaromandaX you're right. let me fix that
– Aniket G
Mar 15 at 4:27
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
Mar 15 at 4:32
@JaromandaX that's actually a very good analogy. Do you mind if I put that in my answer?
– Aniket G
Mar 15 at 4:34
Feel free - it's one of my better ones :p
– Jaromanda X
Mar 15 at 4:35
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 bindthis
only if it's defined in the object though
– somebody
Mar 15 at 6:15
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
Mar 15 at 4:29
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
Mar 15 at 4:29
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 Mar 15 at 4:25
answered Mar 15 at 4:19
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
Mar 15 at 4:29
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
Mar 15 at 4:29
@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
Mar 15 at 4:29
@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
Mar 15 at 4:29
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
Mar 15 at 4:27
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
Mar 15 at 4:27
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 Mar 15 at 4:26
answered Mar 15 at 4:24
Jon BlackJon Black
860718
860718
absolutely no difference in this context - in other context, the difference is what the arrow function returns
– Jaromanda X
Mar 15 at 4:27
add a comment |
absolutely no difference in this context - in other context, the difference is what the arrow function returns
– Jaromanda X
Mar 15 at 4:27
absolutely no difference in this context - in other context, the difference is what the arrow function returns
– Jaromanda X
Mar 15 at 4:27
absolutely no difference in this context - in other context, the difference is what the arrow function returns
– Jaromanda X
Mar 15 at 4:27
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
this
does exist, otherwise the OP would get errors -this
is from the bounding lexical scope
– Jaromanda X
Mar 15 at 4:26
@JaromandaX you're right. let me fix that
– Aniket G
Mar 15 at 4:27
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
Mar 15 at 4:32
@JaromandaX that's actually a very good analogy. Do you mind if I put that in my answer?
– Aniket G
Mar 15 at 4:34
Feel free - it's one of my better ones :p
– Jaromanda X
Mar 15 at 4:35
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
this
does exist, otherwise the OP would get errors -this
is from the bounding lexical scope
– Jaromanda X
Mar 15 at 4:26
@JaromandaX you're right. let me fix that
– Aniket G
Mar 15 at 4:27
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
Mar 15 at 4:32
@JaromandaX that's actually a very good analogy. Do you mind if I put that in my answer?
– Aniket G
Mar 15 at 4:34
Feel free - it's one of my better ones :p
– Jaromanda X
Mar 15 at 4:35
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 Mar 15 at 4:36
answered Mar 15 at 4:24
Aniket GAniket G
2,4211528
2,4211528
1
this
does exist, otherwise the OP would get errors -this
is from the bounding lexical scope
– Jaromanda X
Mar 15 at 4:26
@JaromandaX you're right. let me fix that
– Aniket G
Mar 15 at 4:27
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
Mar 15 at 4:32
@JaromandaX that's actually a very good analogy. Do you mind if I put that in my answer?
– Aniket G
Mar 15 at 4:34
Feel free - it's one of my better ones :p
– Jaromanda X
Mar 15 at 4:35
add a comment |
1
this
does exist, otherwise the OP would get errors -this
is from the bounding lexical scope
– Jaromanda X
Mar 15 at 4:26
@JaromandaX you're right. let me fix that
– Aniket G
Mar 15 at 4:27
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
Mar 15 at 4:32
@JaromandaX that's actually a very good analogy. Do you mind if I put that in my answer?
– Aniket G
Mar 15 at 4:34
Feel free - it's one of my better ones :p
– Jaromanda X
Mar 15 at 4:35
1
1
this
does exist, otherwise the OP would get errors - this
is from the bounding lexical scope– Jaromanda X
Mar 15 at 4:26
this
does exist, otherwise the OP would get errors - this
is from the bounding lexical scope– Jaromanda X
Mar 15 at 4:26
@JaromandaX you're right. let me fix that
– Aniket G
Mar 15 at 4:27
@JaromandaX you're right. let me fix that
– Aniket G
Mar 15 at 4:27
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
Mar 15 at 4:32
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
Mar 15 at 4:32
@JaromandaX that's actually a very good analogy. Do you mind if I put that in my answer?
– Aniket G
Mar 15 at 4:34
@JaromandaX that's actually a very good analogy. Do you mind if I put that in my answer?
– Aniket G
Mar 15 at 4:34
Feel free - it's one of my better ones :p
– Jaromanda X
Mar 15 at 4:35
Feel free - it's one of my better ones :p
– Jaromanda X
Mar 15 at 4:35
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 Mar 15 at 4:37
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 bindthis
only if it's defined in the object though
– somebody
Mar 15 at 6:15
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 bindthis
only if it's defined in the object though
– somebody
Mar 15 at 6:15
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 Mar 15 at 4:40
ZearaeZZearaeZ
710518
710518
it will bindthis
only if it's defined in the object though
– somebody
Mar 15 at 6:15
add a comment |
it will bindthis
only if it's defined in the object though
– somebody
Mar 15 at 6:15
it will bind
this
only if it's defined in the object though– somebody
Mar 15 at 6:15
it will bind
this
only if it's defined in the object though– somebody
Mar 15 at 6:15
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 Mar 15 at 5:03
Bathri NathanBathri Nathan
24918
24918
add a comment |
add a comment |
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
Mar 15 at 4:12
2
this
keyword functions differently in arrow functions. Read this section of the documentation.– Yong Quan
Mar 15 at 4:19
3
Aside - Consider using
el.classList.add('grab')
(andel.classList.remove('grab')
) instead of manipulating the string of class names manually. more info– James
Mar 15 at 4:27
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
Mar 15 at 4:31