Table of Contents
Back Button
Back to Chapter
Control Statements
No items found.
Function & Events
No items found.
Array and its Types
No items found.
OOP : Object Oriented Programming
No items found.
Javascript Standards
No items found.
HTML DOM
No items found.
Javascript : Advanced 1
No items found.
Javascript : Advanced 2
No items found.
Additional JavaScript
No items found.

Getter & Setter

In This Section, We will study about Accessor Property.

Accessor Property are methods that are used to get or set the value of Object.

Accessor Property does exactly what you have learned in previous sections to get and set new methods and properties.

But, There are many Benefits of Using Get & Set

Why Using Getters and Setters?

  • It makes the syntax simple.
  • It is useful for getting and setting properties directly in the Objects.
  • It makes data better and secure.

Declaration

In Accessor Property We have 2 Keywords get and set

get - It is used to get the properties of Objects, this method is known as Getter method.

set - It is used to set the properties of Objects, this method is known as Setter method.

Getter Method

Syntax

const objectName = { // Property get methodName(){ return this.propertyName; } } console.log(objectName.methodName);

get is a Method but It is called directly in the same method as property.

Example

const identity = { name: 'John', age : 20, get welcomeText(){ return "Hello!!! I am " + this.name + " , I am " + this.age + " years old"; } } console.log(identity.welcomeText); // Output // Hello!!! I am John , I am 20 years old

Here,

We have used get accessor property method to show welcomeText

Setter Method

Syntax

const objectName = { // Property set methodName(newValue){ this.property = newValue; } } objectName.methodName = newValue;

get is a Method but It is called directly in the same method as property.

Example

const identity = { name: 'John', age : 20, set changeName(newName){ this.name = newName; } } identity.changeName = "Ram"; console.log(identity.name); //print value of name // Output // Ram

Here,

We have used set accessor property method to change the Name of identity.

set method must have 1 parameter

Object.defineProperty()

By Using defineProperty we can define new properties and methods directly on the object with get and set Keywords.

Syntax

Object.defineProperty(objectName, methodName, { get : function () { // Get Parameters } });

Here,

objectName is the Name of the Object.

methodName is the Name of the Method to create in the Object.

Example

const identity = { name: 'John', age : 30 } Object.defineProperty(identity, 'changeName',{ set : function(newName){ this.name = newName; } }); Object.defineProperty(identity, 'welcomeText',{ get : function(){ return "Hello!!! I am " + this.name + " , I am " + this.age + " years old"; } }); var newName = prompt("Enter your Name") identity.changeName = newName; console.log(identity.welcomeText); // Output // Hello!!! I am Ram, I am 30 years old

Here,

You can see we have create 1 get and 1 set Method.

Here,

Browser will ask for Name in a Prompt Box and your name will be displayed in the Output.

Ask queries
Contact Us on Whatsapp
Hi, How Can We Help You?