Skip to content

JavaScript Getter

Why javascript getter?

We can say that it is the function or method used to get Getters variable values. The Getter concept is common in computer programming: almost all high-level programming languages, including JS, come with a syntax for getter.


What is Encapsulation?

If you have learned an object-oriented programming language, you have heard this statement. First of all I will talk about this term and connect it to getter setter methods. Some features and functions may need to be stored from users first when writing programs. So let’s give an example of everyday life and understand better. We can compare the encapsulation to a phone. The phone buttons are in the middle, but many parts are hidden in the phone. The user is prompted to press only the keys. We can do this by encapsulation. Encapsulation gives the programmer the power to protect objects.

Encapsulation
Encapsulation

Because of this situation getter and setter methods have emerged. “Setter” is used to write data to a hidden variable and “getter” to read data.


When to use getters and setters?

If you need to store the data in the project you have to use getter and setter. Maybe at first sight, it may not be easy. However, after understanding the logic of work, getter and setter are very useful.


Note the following when working with Get Syntax:

  • It must have completely zero parameters.
  • With another get for the same property, or with a data entry, it should not appear in a literal object
  • It can have a number or array descriptor.

JavaScript Getter Examples

Example 1

        let user = {
            username: "alex",
            password: "allen",
            get uName() {
                return this.username;
            },
            get pWord() {
                return this.password;
            }
        }

        console.log(user.uName);
        console.log(user.pWord);

output:

alex
allen


Example 2

        class myClass {
            get message() {
                return "hello javascript"
            }
        }

        let arr = new myClass();
        console.log(arr.message)

output:

hello javascript


Example 3

        var obj = {
            foo: 'this is the value of foo',
            getFoo: function () {
                return this.foo;
            }
        }

        console.log(obj.getFoo());

output:

this is the value of foo


Example 4

        var obj = {
            n: 67,
            get id() {
                return 'The ID is: ' + this.n;
            }
        }

        console.log(obj.id);

output:

The ID is: 67


Example 5

        let people = {
            person1: "rick",
            person2: "oliver",
            get printName() {
                return console.log("name1: " + this.person1 + " / "
                    + "name2: " + this.person2)
            }
        };

        people.printName;

output:

name1: rick / name2: oliver


Browser Support

Chrome1
Edgeyes
Firefox2
Internet Explorer9
Opera9.5
Safari3
Android webview1
Chrome for Android18
Edge mobileyes
Firefox for Android4
Opera Androidyes

1 Comment »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: