检查某个类的对象的值是否已在JavaScript中更改,并基于该值更新另一个值?
要对此进行检查,请使用getter的概念,即get属性。以下是代码-
示例
class Student{
constructor(studentMarks1, studentMarks2){
this.studentMarks1 = studentMarks1
this.studentMarks2 = studentMarks2
var alteredValue = this;
this.getValues = {
get studentMarks1() {
return alteredValue.studentMarks1
},
get studentMarks2() {
return alteredValue.studentMarks2
}
}
}
}
var johnSmith = new Student(78,79)
console.log("Before incrementing the result is=")
console.log("StudentMarks1="+johnSmith.studentMarks1,"StudentMarks2="+johnSmith.studentMarks2);
johnSmith.studentMarks2+=10;
console.log("After incrementing the value 10 in the studentMarks2, the result is as follows=")
console.log("StudentMarks1="+johnSmith.studentMarks1,
"StudentMarks2="+johnSmith.getValues.studentMarks2);要运行以上程序,您需要使用以下命令-
node fileName.js.
在这里,我的文件名为demo200.js。
输出结果
这将产生以下输出-
PS C:\Users\Amit\javascript-code> node demo200.js Before incrementing the result is= StudentMarks1=78 StudentMarks2=79 After incrementing the value 10 in the studentMarks2, the result is as follows= StudentMarks1=78 StudentMarks2=89
热门推荐
10 祝亲友祝福语大全简短
11 祝贺升职文案祝福语简短
12 鲜花礼物结婚祝福语简短
13 祝福语简短文案梳子
14 寿宴文案短句祝福语简短
15 回复群内祝福语简短
16 幼师辞职后祝福语简短
17 朋友领证花束祝福语简短
18 对患者生日祝福语简短