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?













6
















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?











share|improve this 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 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





    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















6
















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?











share|improve this 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 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





    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













6












6








6









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?











share|improve this question















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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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





    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





    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') (and el.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












6 Answers
6






active

oldest

votes


















7














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.






share|improve this answer

























  • @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


















0














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






share|improve this answer

























  • absolutely no difference in this context - in other context, the difference is what the arrow function returns

    – Jaromanda X
    Mar 15 at 4:27


















0














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






share|improve this answer




















  • 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


















0














 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'.






share|improve this answer






























    0














    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.






    share|improve this answer























    • it will bind this only if it's defined in the object though

      – somebody
      Mar 15 at 6:15



















    0














    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.






    share|improve this answer





























      6 Answers
      6






      active

      oldest

      votes








      6 Answers
      6






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      7














      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.






      share|improve this answer

























      • @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















      7














      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.






      share|improve this answer

























      • @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













      7












      7








      7







      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.






      share|improve this answer















      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.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      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

















      • @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













      0














      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






      share|improve this answer

























      • absolutely no difference in this context - in other context, the difference is what the arrow function returns

        – Jaromanda X
        Mar 15 at 4:27















      0














      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






      share|improve this answer

























      • absolutely no difference in this context - in other context, the difference is what the arrow function returns

        – Jaromanda X
        Mar 15 at 4:27













      0












      0








      0







      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






      share|improve this answer















      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







      share|improve this answer














      share|improve this answer



      share|improve this answer








      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

















      • 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











      0














      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






      share|improve this answer




















      • 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















      0














      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






      share|improve this answer




















      • 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













      0












      0








      0







      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






      share|improve this answer















      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







      share|improve this answer














      share|improve this answer



      share|improve this answer








      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












      • 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











      0














       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'.






      share|improve this answer



























        0














         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'.






        share|improve this answer

























          0












          0








          0







           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'.






          share|improve this answer













           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'.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 15 at 4:37









          Kaushal RegmiKaushal Regmi

          538




          538





















              0














              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.






              share|improve this answer























              • it will bind this only if it's defined in the object though

                – somebody
                Mar 15 at 6:15
















              0














              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.






              share|improve this answer























              • it will bind this only if it's defined in the object though

                – somebody
                Mar 15 at 6:15














              0












              0








              0







              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.






              share|improve this answer













              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.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Mar 15 at 4:40









              ZearaeZZearaeZ

              710518




              710518












              • 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

















              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












              0














              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.






              share|improve this answer



























                0














                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.






                share|improve this answer

























                  0












                  0








                  0







                  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.






                  share|improve this answer













                  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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 15 at 5:03









                  Bathri NathanBathri Nathan

                  24918




                  24918













                      Popular posts from this blog

                      How should I support this large drywall patch? Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?How do I cover large gaps in drywall?How do I keep drywall around a patch from crumbling?Can I glue a second layer of drywall?How to patch long strip on drywall?Large drywall patch: how to avoid bulging seams?Drywall Mesh Patch vs. Bulge? To remove or not to remove?How to fix this drywall job?Prep drywall before backsplashWhat's the best way to fix this horrible drywall patch job?Drywall patching using 3M Patch Plus Primer

                      random experiment with two different functions on unit interval Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)Random variable and probability space notionsRandom Walk with EdgesFinding functions where the increase over a random interval is Poisson distributedNumber of days until dayCan an observed event in fact be of zero probability?Unit random processmodels of coins and uniform distributionHow to get the number of successes given $n$ trials , probability $P$ and a random variable $X$Absorbing Markov chain in a computer. Is “almost every” turned into always convergence in computer executions?Stopped random walk is not uniformly integrable

                      Lowndes Grove History Architecture References Navigation menu32°48′6″N 79°57′58″W / 32.80167°N 79.96611°W / 32.80167; -79.9661132°48′6″N 79°57′58″W / 32.80167°N 79.96611°W / 32.80167; -79.9661178002500"National Register Information System"Historic houses of South Carolina"Lowndes Grove""+32° 48' 6.00", −79° 57' 58.00""Lowndes Grove, Charleston County (260 St. Margaret St., Charleston)""Lowndes Grove"The Charleston ExpositionIt Happened in South Carolina"Lowndes Grove (House), Saint Margaret Street & Sixth Avenue, Charleston, Charleston County, SC(Photographs)"Plantations of the Carolina Low Countrye