如何在类中的数组对象中编辑值 - 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

更新于: 03-Oct-2020

214 次浏览

开启你的 职业

完成课程以获得认证

开始
广告
© . All rights reserved.