如何在類中的陣列物件中編輯值 - JavaScript?
為此,使用“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
廣告