- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if value of an object of certain class has been altered in JavaScript and update another value based on it?
To check this, use the concept of getter, i.e. the get property. Following is the code −
Example
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);
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo200.js.
Output
This will produce the following output −
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
- Related Articles
- JavaScript - Sort key value pair object based on value?
- Update a table based on StudentId value in MySQL?
- Sort object array based on another array of keys - JavaScript
- How to check if an element has a certain class name with jQuery?
- Perform MySQL UPDATE on the basis of DATE value in another column
- How to check if an object is an instance of a Class in JavaScript?
- Splitting an array based on its first value - JavaScript
- Check if a value exists in an array and get the next value JavaScript
- Compute the truth value of an array AND to another array element-wise based on conditions in Numpy
- Check if any value in an R vector is greater than or less than a certain value.
- Update MongoDB field using value of another field?
- map() array of object titles into a new array based on other property value JavaScript
- How to check if a value is object-like in JavaScript?
- Compute the truth value of an array XOR another array element-wise based on conditions in Numpy
- How to check if a string has a certain piece of text in JavaScript?

Advertisements