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

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 :: Function Recurser / Infinit Calling 
Javascript :: push code from vscode using CL 
Javascript :: Copy an Array with the Spread Operator 
Javascript :: mongoose return full object after inserting data to db 
Javascript :: aos cdn 
Javascript :: js how to find not unic values in array 
Javascript :: javascript etaretot 
Javascript :: firebase check if key exists javascript 
Javascript :: Caused by: PG::ConnectionBad: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"? postgres app 
Javascript :: set prop as optional in react flow 
Javascript :: onclick clear input field javascript 
Javascript :: validator.contains 
Javascript :: Use ChainLink Feed Registry 
Javascript :: change previous location history javascript 
Javascript :: how to generate password hash and a salt in nodejs 
Javascript :: SHOPIFY CUSTOMER WITHOUT REGISTRATION 
Javascript :: cd doesn’t work inside childProcess 
Javascript :: useRef is not working with custom compnents 
Javascript :: js hk 
Javascript :: combine 2 data in column 
Javascript :: convert File to multer file js 
Javascript :: solo letras js 
Javascript :: jquery init dropdown 
Javascript :: asynchronous file reading 
Javascript :: Javascript Recursion shuffle card 
Javascript :: Backbone Models In Collection Is Added Here 
Javascript :: asp.net updatepanel autoscroll fix 
Javascript :: Get Error 
Javascript :: javascript Program for sum of arithmetic series using loop 
Javascript :: React clock via props 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =