如何编辑类中数组内对象的值 - JavaScript?

javascriptweb developmentfront end technologyobject oriented programming更新于 2026/1/6 3:56:17

为此,请使用“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

相关文章


有用资源