如何在一个类的JavaScript中编辑对象的值-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