In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use getters and setter in JavaScript". The content in the article is simple and clear, easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use getters and setter in JavaScript".
Getter and setter exist in most object-oriented programming languages, including JavaScript. They are code constructs that help developers access the properties of objects in a secure manner. With getter, you can access ("get") the values of properties from external code, while setter allows you to change ("set") their values. We will show you how to create getter and setter in JavaScript.
JavaScript objects can have multiple properties and methods that store static data and dynamic functions. Properties are static key-value pairs, while methods are object-specific functions.
For example, Car.color can be a property, and Car.drive () can be a method of a Car object. With getter, you can access the color property of the Car object, and with setter, you can modify its value (for example, from blue to black).
With getter and setter, you can only get and set the value of the property, not the method, because the method is not static.
You can create getter and setter in three different ways:
Use the default method syntax (getter and setter methods)
Use the get and set keywords
Use the Object.defineProperty () method.
Getters and Setters
Getters and Setters allow you to quickly get or set the data of an object. In general, an object has two methods that are used to get and set a value, such as:
{
GetValue: function () {
Return this._value
}
SetValue: function (val) {
This._value = val
}
}
One of the obvious benefits of writing JavaScript in this way is that you can use it to hide properties that you don't want to be accessed directly by the outside world.
Inspiratory method
The easiest way to get object properties is to define the getter using the default method syntax for each property. Let's look at an example of how to create a getter using this technique. The MYCAR object has two properties: color and production. This is how you create it using object text:
Var myCar = {
Color: "blue"
Make: "Toyota"
}
If desired, you can access the color directly and create attributes using the following code:
MyCar.color
/ / Blue
MyCar.make
/ / Toyota
Although this technique works, there are many advantages to using getter instead of calling properties directly. For example, you can perform an action or check on a property (such as an if-else statement) before retrieving its value.
The most important thing about the get keyword is that it defines an accessor property, not a method. Therefore, it cannot have the same name as the data property that stores the value it accesses. In the above code, defColor and defMake are data properties, while color and make are accessor properties.
This is why you need to use attribute syntax that does not use parentheses after the getter name to call getter (for example, myCar.color).
Because older versions of IE8 do not support the get keyword, use the getter method if you need to support older browsers.
Thank you for your reading, the above is the content of "how to use getters and setter in JavaScript". After the study of this article, I believe you have a deeper understanding of how to use getters and setter in JavaScript. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.