Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the JavaScript accessor

2025-02-24 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report