Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript Getters and Setters

class Person {
    constructor(name) {
        this.name = name;
    }
    // getter
    get personName() {
        return this.name;
    }
    // setter
    set personName(x) {
        this.name = x;
    }
}
let person1 = new Person('Jack');
console.log(person1.name); // Jack

// changing the value of name property
person1.personName = 'Sarah';
console.log(person1.name); // Sarah
Comment

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

getters and setters javascript

let obj = {
  log: ['a', 'b', 'c'],
  get latest() {
    if (this.log.length === 0) {
      return undefined;
    }
    return this.log[this.log.length - 1];
  }
};

obj.log.push('d');
console.log(obj.latest); //output: 'd'
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
Javascript :: js how to find max value in an array 
Javascript :: javscript assert 
Javascript :: add font awesome with nextjs 
Javascript :: react-native-bouncy-checkbox 
Javascript :: javascript escape character 
Javascript :: linking open app settings 
Javascript :: javascript fetch api 
Javascript :: chain id 
Javascript :: install axios nodejs 
Javascript :: string length js 
Javascript :: moment.js format 
Javascript :: get string before specific character nodejs 
Javascript :: How to replace an array vue.js 
Javascript :: postmark with nodejs 
Javascript :: find multiples of a number 
Javascript :: bind in javascript 
Javascript :: sails disable grunt 
Javascript :: Sorting Data Accessor 
Javascript :: uncaughtException javascript 
Javascript :: object find javascript 
Javascript :: google translate javascript 
Javascript :: datatable on error.dt 
Javascript :: properly print json in colab 
Javascript :: jquery autocomplete search 
Javascript :: changing photo with js 
Javascript :: flask vue.js not working 
Javascript :: setTilme out js 
Javascript :: javascript Strict Mode in Variable 
Javascript :: jsfuck 
Javascript :: nodejs get prosses id 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =