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 :: how to include script file in javascript with javascript 
Javascript :: how to export multiple functions react from one file 
Javascript :: javascript add to object 
Javascript :: how can we open page on new tab in angular or js or typescript 
Javascript :: react got error need to enable javascript 
Javascript :: jquery selector class child 
Javascript :: Could not resolve project :react-native-iap mergedebugassets 
Javascript :: yarn add node-sass webpacker error rails 
Javascript :: nan javascript 
Javascript :: d3 not reading json 
Javascript :: react mid senior dev interview questuions 
Javascript :: angularjs onclick disable button click 
Javascript :: React-redux and redux 
Javascript :: Minimal Project Angular 
Javascript :: axios in functional component 
Javascript :: move last element of array to begining javascript 
Javascript :: remove falsy values from array with lodash 
Javascript :: react props have changed method 
Javascript :: how to convert draftjs content to html 
Javascript :: react native data map is not a function 
Javascript :: leaflet remove layergroup 
Javascript :: factorial js 
Javascript :: startswith vowels in js 
Javascript :: angular disable select dropdown 
Javascript :: how to make bootstrap navbar to change on scroll 
Javascript :: global axios vue 2 
Javascript :: express.js hello world 
Javascript :: print array without brackets javascript 
Javascript :: ANGULAR locale fr 
Javascript :: redux toolkit how to set empty initial state 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =