Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

what is getter and setter in javascript

// 1) In JavaScript, getter methods are used to access the properties of an object.
// we use get keyword to define a getter method to get or access the property value
const student = {
    // data property
    firstName: 'Chetan',
 
    // accessor property(getter)
    get getName() {
        return this.firstName;
    }
};

// accessing data property
console.log(student.firstName); // Chetan

// accessing getter methods or accessing the value as a property
console.log(student.getName); // Chetan

// trying to access as a method
console.log(student.getName()); // error

// In the above program, a getter method getName() is created to access the property of an object.
// Note: To create a getter method, the get keyword is used.

// 2) In JavaScript, setter methods are used to change the values of an object.
// we use set keyword to define a setter method to set the property value

const student = {
    firstName: 'Chetan',
    
    //accessor property(setter)
    set changeName(newName) {
        this.firstName = newName;
    }
};

console.log(student.firstName); // Chetan

// change(set) object property using a setter
student.changeName = 'Jeny';

console.log(student.firstName); // Jeny
// In the above example, the setter method is used to change the value of an object.
// Note: To create a setter method, the set keyword is used.
Comment

what is getter and setter ?

public class Computer
    {
        int ram;
        public int RAM 
        { 
             get 
             {
                  return ram;
             }
             set 
             {
                  ram = value; // value is a reserved word and it is a variable that holds the input that is given to ram ( like in the example below )
             }
        }
     }
Comment

PREVIOUS NEXT
Code Example
Java :: bukkit java get max players 
Java :: Java List Replace at Index using set() function 
Java :: java map get value 
Java :: default constructor java 
Java :: Rotate Vector by an angle 
Java :: java byte array to blob 
Java :: java define interface 
Java :: bufferedreader 
Java :: arraylist add method 
Java :: get last day of month java 
Java :: java method exercises 
Java :: @entity annotation in spring boot 
Java :: java program to print vowels in a string 
Java :: Java if (if-then) Statement 
Java :: java float precision 
Java :: number of matches regex java 
Java :: get my jre path 
Java :: java string copy characters 
Java :: how do I test the reverse method in java using jest 
Java :: java scanner tokens with withespace 
Sql :: mysql create user 
Sql :: postgresql update sequence next value 
Sql :: Failed to stop mysqld.service: Unit mysqld.service not loaded. 
Sql :: put line oracle 
Sql :: mysql show databases 
Sql :: drop all table postgresql 
Sql :: oracle kill session 
Sql :: find string in stored procedure sql server 
Sql :: mysql server does not start mac 
Sql :: get the list of all tables in sql server 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =