Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Sửa giá trị object bên trong mảng trong class - JavaScript

Dùng this trong method của object bên trong mảng để sửa thuộc tính.

Mã thực hiện

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);

Kết quả

The Change First Name is=Carol