이를 확인하려면 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