Horje Website
JavaScript this Keyword Value inside an Arrow Function – JavaScript Code

JavaScript this Keyword Value inside an Arrow Function

JavaScript this Keyword Value inside an Arrow Function - JavaScript Code


 
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JavaScript this Keyword Value inside an Arrow Function</title>
  6. </head>
  7. <body>
  8. <script>
  9. function Person(nickname, country) {
  10. this.nickname = nickname;
  11. this.country = country;
  12. this.getInfo = function() {
  13. // Outer function context (Person object)
  14. return () => {
  15. // Inner function context (Person object)
  16. document.write(this.constructor.name); // Person
  17. document.write(`<p>Hi, I'm ${this.nickname} from ${this.country}</p>`);
  18. };
  19. }
  20. }
  21. let p = new Person('Rick', 'Argentina');
  22. let printInfo = p.getInfo();
  23. printInfo(); // Hi, I'm Rick from Argentina
  24. </script>
  25. </body>
  26. </html>



Try Your Self ->



Published:
September 12, 2020
Author:
admin
Category:
JavaScript Example, Javascript Source Code
Views:
53

This article was posted in JavaScript Example, Javascript Source Code and tagged , , . Bookmark the permalink. Follow comments with the RSS feed for this post.Post a Comment or leave a trackback: Trackback URL.

Leave a Reply

Your email address will not be published. Required fields are marked *

 

Horje © 2011 - 2023