Perbedaan karakteristik dari arrow function dan regular function selanjutnya ada pada penggunaan keyword this. Penjelasan dari this sendiri menyusul di materi class. Namun kita akan bahas sedikit mengenai ini untuk menggambarkan perbedaan ketika this digunakan oleh arrow function dan regular function.
Jika sebuah regular function dipanggil dengan menggunakan keyword new. Maka nilainya akan menjadi objek, contohnya:

  1. function People(name, age, hobby) {

  2.     this.name = name;

  3.     this.age = age;

  4.     this.hobby = hobby;

  5. }

  6.  

  7. const programmer = new People("John", 18, ["Coding", "Read book", "Ping-pong"]);

  8.  

  9. console.log(programmer.name);

  10. console.log(programmer.age);

  11. console.log(programmer.hobby);

  12.  

  13. /* output:

  14. John

  15. 18

  16. [ 'Coding', 'Read book', 'Ping-pong' ]

  17. */


Objek yang dibuat menggunakan function dengan keyword new, sama halnya seperti kita membuat objek seperti menggunakan objek literals { }.

  1. const programmer = {

  2.     name: "John",

  3.     age: 18,

  4.     hobby: ["Coding", "Read book", "Ping-Pong"]

  5. }

  6.  

  7. console.log(programmer.name);

  8. console.log(programmer.age);

  9. console.log(programmer.hobby);

  10.  

  11. /* output:

  12. John

  13. 18

  14. [ 'Coding', 'Read book', 'Ping-pong' ]

  15. */


Pada objek, this keyword mengembalikan nilai objeknya sendiri. this dapat digunakan untuk mengelola properti pada objeknya. Namun jika fungsi dipanggil tanpa menggunakan keyword new, this akan memiliki nilai global object (Window jika di browser).
Sedangkan fungsi yang dibuat dengan menggunakan gaya arrow tidak akan pernah memiliki nilai this, yang artinya kita tidak pernah bisa membuat objek menggunakan arrow function. Jika kita menggunakan this pada arrow function maka nilai this tersebut merupakan nilai objek di mana arrow function itu berada.
Perhatikan kedua contoh kode berikut:

  1. function People(name, age, hobby) {

  2.     this.name = name;

  3.     this.age = age;

  4.     this.hobby = hobby;

  5. }

  6.  

  7.  

  8. // menambahkan introMyself ke People

  9. People.prototype.introMyself = function () {

  10.     // this -> People

  11.     setTimeout(function() {

  12.        // this -> ??

  13.         console.log(`Hello! Nama saya ${this.name}, umur saya ${this.age}.`)

  14.         console.log(`Hobby saya adalah ${this.hobby}`)

  15.     }, 300)

  16. }

  17.  

  18.  

  19. const programmer = new People("John", 18, ["Coding", "Read book", "Ping-pong"]);

  20. programmer.introMyself();

  21.  

  22.  

  23. /* output:

  24. Hello! Nama saya undefined, umur saya undefined.

  25. Hobby saya adalah undefined

  26. */




  1. function People(name, age, hobby) {

  2.     this.name = name;

  3.     this.age = age;

  4.     this.hobby = hobby;

  5. }

  6.  

  7.  

  8. // menambahkan introMyself ke People

  9. People.prototype.introMyself = function () {

  10.     // this -> People

  11.     setTimeout(() => {

  12.         // this -> People

  13.         console.log(`Hello! Nama saya ${this.name}, umur saya ${this.age}.`)

  14.         console.log(`Hobby saya adalah ${this.hobby}`)

  15.     }, 300)

  16. }

  17.  

  18.  

  19. const programmer = new People("John", 18, ["Coding", "Read book", "Ping-pong"]);

  20. programmer.introMyself();

  21.  

  22.  

  23.  

  24.  

  25. /* output:

  26. Hello! Nama saya John, umur saya 18.

  27. Hobby saya adalah Coding,Read book,Ping-pong

  28. */


Fungsi yang dituliskan di dalam setTimeout() dipanggil tanpa new. Itu berarti nilai dari this jika digunakan di dalam fungsi tersebut adalah global object. Itulah mengapa output akan menghasilkan nilai undefined ketika properti name, age, dan hobby dipanggil.
Berbeda ketika kita menuliskan arrow function di dalam setTimeout(), nilai this memiliki nilai objek sesuai dengan konteksnya (People). Arrow function akan sangat berguna untuk kasus seperti ini

No image
Bookmark

Penggunaan This dalam Arrow Function di Javascript

Type:
Genre:
Status:
Country:
Keywords: Watch online Penggunaan This dalam Arrow Function di Javascript bahasa Indonesia, Streaming anime Penggunaan This dalam Arrow Function di Javascript sub indo, Mau nonton anime terbaik dengan subtitle Indonesia? Streaming anime Jepang dari anime Penggunaan This dalam Arrow Function di Javascript, website streaming anime subtitle indonesia dan nonton anime indo update setiap hari, TV Penggunaan This dalam Arrow Function di Javascript, Terbaru Terupdate dan Terlengkap dengan Kualitas 1080p, 720p. 480p, 360p Anime Sub Indo.

Comments

Comment Report Rule
Show Comments
9Anime Comment Policies & Guidelines
  1. Spoiling Content

    Definition

    “Anime Comment” is a spoiler-free section and is usually used for a general discussion about anime without spoiling any content.

    “Episode Comment” may contain spoilers for current or previous episodes. Contents from manga or future episodes are considered as spoilers.

    Hiding spoilers with spoiler tags is not allowed either.

    Consequences

    Minor Spoiler:-

    1st Offense: Warning + Temporary Ban (Ranging 1 day to 3 days)

    2nd Offense Within 30 Days of 1st Offense: Permanent Ban

    Major Spoiler:-

    1st Offense: Permanent Ban

  2. Diversion From Topic

    Definition

    The comment section should be about the anime on hand. Although references from other anime may be used, the core topic of the comments should be about the anime at hand.

    This rule also applies on advertising other streaming websites which results in an instant ban.

    This rule also applies to spam, posting comments like “first”, “second”, etc. are considered spam.

    Consequences

    1st Offense: Warning + Temporary Ban (Ranging 1 day to 3 days)

    2nd Offense Within 30 Days of 1st Offense: Permanent Ban

  3. Flame War

    Definition

    Exchange of angry or abusive messages between the comments. Any form of insulting is also considered as flame war.

    Consequences

    1st Offense: Warning + Temporary Ban (Ranging 1 day to 3 days)

    2nd Offense Within 30 Days of 1st Offense: Permanent Ban

  4. Posting Harmful Link

    Definition

    Post of hyperlinks for websites that may contain viruses or may harm your PC or device.

    Consequences

    1st Offense: Permanent Ban + Deletion of All Comments

  5. Posting NSFW Content

    Definition

    Posting contents that are not safe for work, such as pornographic images or video, ecchi anime, hentai, etc.

    Consequences

    1st Offense: Warning + Temporary Ban (Ranging 1 day to 3 days)

    2nd Offense Within 30 Days of 1st Offense: Permanent Ban

  6. Inappropriate Profile Picture

    Definition

    Using a profile picture that is not safe for work. Although minor ecchi or fanservice is acceptable.

    Consequences

    1st Offense: Warning + Temporary Ban (Ranging 1 day to 3 days)

    2nd Offense Within 30 Days of 1st Offense: Permanent Ban

  7. Use of Common Sense

    Definition

    If you think that you’ll get in trouble for what you’re about to do, don’t do it.

    Consequences

    1st Offense: Warning + Temporary Ban (Ranging 1 day to 3 days)

    2nd Offense Within 30 Days of 1st Offense: Permanent Ban

  8. Upvote Abusing

    Definition

    Creating multiple alternative accounts to upvote your own comments.

    Consequences

    1st Offense: Permanent Ban on All Accounts + Deletion of All Comments

  9. Downvote Abusing

    Definition

    Targeting a user and downvoting all his comments, or creating multiple accounts to downvote one or more comments.

    Consequences

    1st Offense: Permanent Ban on All Accounts

  10. Swearing

    Definition

    Swearing is fine, as long as it’s not directed towards anyone, even as a joke. It may be misinterpreted by a moderator.

    Consequences

    1st Offense: Warning + Temporary Ban (Ranging 1 day to 3 days)

    2nd Offense Within 30 Days of 1st Offense: Permanent Ban

  11. Moderator Verdict

    Definition

    A moderator’s verdict is final and arguing with them will only cause further punishment. If you have a complaint against a staff member, or wish to file a ban appeal, go to the 9Anime Discord Server and post your issue in the #disqus-appeals channel.

    Consequences

    1st Offense: Warning + Temporary Ban (Ranging 1 day to 3 days)

    2nd Offense Within 30 Days of 1st Offense: Permanent Ban

Think you have been unfairly warned or banned?

If you believe that you’ve been unfairly banned or if you have a complaint against one or more Disqus Staff Members, you may post an appeal or complaint on 9Anime Discord Server.

STEPS

  1. Join 9Anime Discord Server: https://discord.gg/xxxxxxx
  2. Read the #rules channel and follow instructions. (Optional, but highly recommended)
  3. Head towards #disqus-appeals channel.
  4. Use the appropriate template to appeal a ban or file a complaint.