setter and getter in javascript code example
Example 1: javascript class getter setter
// *** JS Class GETTER / SETTER (+split)
class Person {
constructor(firstname, lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
// getters => access properties
// setters => change or mutate them
get fullName() {
return `Hello ${this.firstname} ${this.lastname}`;
}
set fullName(space) {
const parts = space.split(' ');
this.firstname = parts[0];
this.lastname = parts[1];
}
}
let run = document.getElementById("run");
run.addEventListener('click', () => {
let john = new Person('John', 'Connor');
console.log(john.fullName);
john.fullName = 'Billy Butcher';
console.log(john.firstname + ' ' + john.lastname);
//console.log(`${john.firstname} ${john.lastname}`); same output
// => has to be john.firstname otherwise undefined
})
// output => Hello John Connor | Billy Butcher
Example 2: 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'
Example 3: setter&getter java
public class Vehicle {
private String color;
// Getter
public String getColor() {
return color;
}
// Setter
public void setColor(String c) {
this.color = c;
}
}
Example 4: getter and setters in js
var person={firstname:"chetha",secondname="kumar",get fullname()
{
return this.firstname+" "+this.secondname;
}
document.write(person.fullname);
var person={firstname:"chethan",secondname="kumar",course:"",set course(x)
{
this.course=x;
}
person.course="bca";
document.write(person.course);
Example 5: setters and getter best example
class Test {
// private variables
private int age;
private String name;
// initialize age
public void setAge(int age) {
this.age = age;
}
// initialize name
public void setName(String name) {
this.name = name;
}
// access age
public int getAge() {
return this.age;
}
// access name
public String getName() {
return this.name;
}
}
class Main {
public static void main(String[] args) {
// create an object of Test
Test test = new Test();
// set value of private variables
test.setAge(24);
test.setName("Programiz");
// get value of private variables
System.out.println("Age: " + test.getAge());
System.out.println("Name: " + test.getName());
}
}
Example 6: Getter and Setter methods
import java.util.Scanner;
class Student {
private String name;
private int age;
Student(){
this.name = "Rama";
this.age = 29;
}
Student(String name, int age){
this.name = name;
this.age = age;
}
public void display() {
System.out.println("name: "+this.name);
System.out.println("age: "+this.age);
}
}
public class AccessData{
public static void main(String args[]) {
//Reading values from user
Scanner sc = new Scanner(System.in);
System.out.println("Enter the name of the student: ");
String name = sc.nextLine();
System.out.println("Enter the age of the student: ");
int age = sc.nextInt();
Student obj1 = new Student(name, age);
obj1.display();
Student obj2 = new Student();
obj2.display();
}
}