คีย์เวิร์ด 'this' ใน JavaScript หมายถึงออบเจ็กต์ที่มันเป็นของ ใน Arrow Function 'this' จะเป็นของ Global Object ภายในฟังก์ชันทั่วไป 'this' อาจจะเป็น undefined แต่ใน Arrow Function จะได้ค่า ที่แน่นอน
ตัวอย่าง
<html> <body> <script> function Student(fname, grade) { this.fname = fname; this.grade = grade; this.details = function() { return () => { document.write(`Hi, I'm ${this.fname} from ${this.grade} grade`); }; } } let info = new Student('picaso', 'seventh'); let printInfo = info.details(); printInfo(); </script> </body> </html>
ผลลัพธ์
Hi, I'm picaso from seventh grade