สำหรับเรื่องนี้ ให้ใช้คีย์เวิร์ด "this"
ตัวอย่าง
class Employee {
constructor() {
this.tempObject = [
{
firstName: "David",
setTheAnotherFirstName() {
this.firstName = "Carol";
}
}
];
}
}
var empObject = new Employee();
empObject.tempObject[0].setTheAnotherFirstName();
console.log("The Change First Name is=" + empObject.tempObject[0].firstName);
เพื่อรันโปรแกรมข้างต้น คุณต้องใช้คำสั่งต่อไปนี้:
node fileName.js.
ที่นี่ ชื่อไฟล์ของฉันคือ demo220.js
ผลลัพธ์
ผลลัพธ์มีดังนี้:
PS C:\Users\Amit\JavaScript-code> node demo220.js The Change First Name is=Carol