Đối với điều này, hãy sử dụng từ khóa “this”.
Ví dụ
Sau đây là mã -
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); Để chạy chương trình trên, bạn cần sử dụng lệnh sau -
node fileName.js.
Đây, tên tệp của tôi là demo220.js.
Đầu ra
Kết quả như sau -
PS C:\Users\Amit\JavaScript-code> node demo220.js The Change First Name is=Carol