(JavaScript Tutorials for Beginners)
In this end-to-end example, you will learn – JavaScript tutorials for Beginners – JavaScript Prototype.
JavaScript Getter and Setter
In this tutorial, you are going to learn about JavaScript getter and setter methods with the help of examples.
In JavaScript, there are two kinds of object properties:
- Data properties
- Accessor properties
Data Property
Here’s an example of data property that we have been using in the previous tutorials.
let student = {
// data property
firstName: 'Monica';
};
Accessor Property
In JavaScript, accessor properties are methods that get or set the value of an object. For that, we use these two keywords:
get
– to define a getter method to get the property valueset
– to define a setter method to set the property value
JavaScript Getter
In JavaScript, getter methods are used to access the properties of an object. For example,
let student = {
// data property
firstName: 'Monica',
//accessor property(getter)
get getName() {
return this.firstName;
}
};
// accessing data property
console.log(student.firstName); // Monica
// accessing getter methods
console.log(student.getName); // Monica
//trying to access as a method
console.log(student.getName()); // error
In the above program, a getter method getName()
is created to access the property of an object.
get getName() {
return this.firstName;
}
Note: To create a getter method, the get
keyword is used.
And also when accessing the value, we access the value as a property.
student.getName;
When you try to access the value as a method, an error occurs.
console.log(student.getName()); // error
JavaScript Setter
In JavaScript, setter methods are used to change the values of an object. For example,
let student = {
firstName: 'Monica',
//accessor property(setter)
set changeName(newName) {
this.firstName = newName;
}
};
console.log(student.firstName); // Monica
// change(set) object property using a setter
student.changeName = 'Sarah';
console.log(student.firstName); // Sarah
In the above example, the setter method is used to change the value of an object.
set changeName(newName) {
this.firstName = newName;
}
Note: To create a setter method, the set
keyword is used.
As shown in the above program, the value of firstName
is Monica
.
Then the value is changed to Sarah
.
student.chageName = 'Sarah';
Note: Setter must have exactly one formal parameter.
JavaScript Object.defineProperty()
In JavaScript, you can also use Object.defineProperty()
method to add getters and setters. For example,
let student = {
firstName: 'Monica'
}
// getting property
Object.defineProperty(student, "getName", {
get : function () {
return this.firstName;
}
});
// setting property
Object.defineProperty(student, "changeName", {
set : function (value) {
this.firstName = value;
}
});
console.log(student.firstName); // Monica
// changing the property value
student.changeName = 'Sarah';
console.log(student.firstName); // Sarah
In the above example, Object.defineProperty()
is used to access and change the property of an object.
The syntax for using Object.defineProperty()
is:
Object.defineProperty(obj, prop, descriptor)
The Object.defineProperty()
method takes three arguments.
- The first argument is the objectName.
- The second argument is the name of the property.
- The third argument is an object that describes the property.
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.
Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners
Latest end-to-end Learn by Coding Projects (Jupyter Notebooks) in Python and R:
All Notebooks in One Bundle: Data Science Recipes and Examples in Python & R.
End-to-End Python Machine Learning Recipes & Examples.
End-to-End R Machine Learning Recipes & Examples.
Applied Statistics with R for Beginners and Business Professionals
Data Science and Machine Learning Projects in Python: Tabular Data Analytics
Data Science and Machine Learning Projects in R: Tabular Data Analytics
Python Machine Learning & Data Science Recipes: Learn by Coding
R Machine Learning & Data Science Recipes: Learn by Coding
Comparing Different Machine Learning Algorithms in Python for Classification (FREE)
There are 2000+ End-to-End Python & R Notebooks are available to build Professional Portfolio as a Data Scientist and/or Machine Learning Specialist. All Notebooks are only $29.95. We would like to request you to have a look at the website for FREE the end-to-end notebooks, and then decide whether you would like to purchase or not.