In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article, the editor introduces in detail "how to use the JavaScript accessor". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to use the JavaScript accessor" can help you solve your doubts.
JavaScript accessor (Getter and Setter)
Why use Getter and Setter?
It provides a more concise syntax
It allows the syntax of properties and methods to be the same
It ensures better data quality.
It is good for backstage work
JavaScript Getter (get keyword)
This example uses the lang property to get the value of the language property.
Example
/ / create an object:
Var person = {
FirstName: "Bill"
LastName: "Gates"
Language: "en"
Get lang () {
Return this.language
}
}
/ / use getter to display data from objects:
Document.getElementById ("demo"). InnerHTML = person.lang
JavaScript Setter (set keyword)
This example uses the lang property to set the value of the language property.
Example
Var person = {
FirstName: "Bill"
LastName: "Gates"
Language: ""
Set lang (lang) {
This.language = lang
}
}
/ / use setter to set object properties:
Person.lang = "en"
/ / display the data from the object:
Document.getElementById ("demo"). InnerHTML = person.language
JavaScript function or Getter?
What is the difference between the following two examples?
Example 1
Var person = {
FirstName: "Bill"
LastName: "Gates"
FullName: function () {
Return this.firstName + "" + this.lastName
}
}
/ / use methods to display data from objects:
Document.getElementById ("demo"). InnerHTML = person.fullName ()
Example 2
Var person = {
FirstName: "Bill"
LastName: "Gates"
Get fullName () {
Return this.firstName + "" + this.lastName
}
}
/ / use getter to display data from objects:
Document.getElementById ("demo"). InnerHTML = person.fullName
Read here, this article "how to use the JavaScript accessor" article has been introduced, want to grasp the knowledge of this article also need to practice and use to understand, if you want to know more about the article, welcome to follow the industry information channel.
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.