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

getters and setters in java script

const student = {

    // data property
    firstName: 'Monica',
    
    // accessor property(getter)
    get getName() {
        return this.firstName;
    },
    //accessor property(setter)
    set setName(newName){
      this.firstName = newName;
  }
};

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

// accessing getter methods
console.log(student.getName); // Monica

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

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

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

Comment

getters and setters

class P:
    def __init__(self, x):
        self.__x = x
    def get_x(self):
        return self.__x
    def set_x(self, x):
        self.__x = x
Comment

getters and setters

For IntelliJ IDEA TO generate getters and setters:
Refactor-->EncapsulatFields 
OR
use Keyboard Shortcut: alt + insert
Comment

js why to use getters and setters

1) Syntax reasons. It’s easier and faster to read code 
created with accessor functions
2) Encapsulation. I can create safer code with accessor functions.
Comment

PREVIOUS NEXT
Code Example
Javascript :: difference 
Javascript :: js contains 
Javascript :: react useeffect hook 
Javascript :: Get async: false 
Javascript :: javascript null Conversion to Number 
Javascript :: javascript encrypt decrypt 
Javascript :: useQuery apollo more than one 
Javascript :: export to csv - Javascript - Download CSV as File 
Javascript :: object assign js 
Javascript :: Substring in Javascript using slice 
Javascript :: const { something} javascript 
Javascript :: ng-lazyload-image 
Javascript :: angular keyframes % 
Javascript :: react linking to documents 
Javascript :: find 401 error and logout axios in react 
Javascript :: js ternaire 
Javascript :: how to add author to javascript 
Javascript :: validate ajax nonce request wordpress 
Javascript :: react-intersection-observer 
Javascript :: sequelize exclude attributes 
Javascript :: elasticsearch response format 
Javascript :: how to call api on load using hooks in react 
Javascript :: cheerio library to parse the meta tags in url 
Javascript :: react native communicate with webview 
Javascript :: delete an item from array javascript 
Javascript :: last index of string in javascript 
Javascript :: Error capturing image. ionic 
Javascript :: js recursive fetch 
Javascript :: Shallow copy Objects using Object.prototype.assign method 
Javascript :: how to check element has event or not in jquery 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =