Answers for "getter and setter in javascript"

6

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
Posted by: Guest on October-21-2020
0

javascript getterand setter

class Thermostat {
  constructor(fahrenheit) {
    this.fahrenheit = fahrenheit;
  }
  
  get temperature() {
    return (5 / 9) * (this.fahrenheit - 32);
  }
  
  set temperature(celsius) {
    this.fahrenheit = (celsius * 9.0) / 5 + 32;
  }
}
Posted by: Guest on April-20-2021
4

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'
Posted by: Guest on May-20-2020
0

javascript getters and setters

/*Getter functions are meant to simply return (get) the value of an object's 
private variable to the user without the user directly accessing the private 
variable.*/
/*Setter functions are meant to modify (set) the value of an object's private 
variable based on the value passed into the setter function. This change could
involve calculations, or even overwriting the previous value completely.*/
class Book {
  constructor(author) {
    this._author = author;
  }
  // getter
  get writer() {
    return this._author;
  }
  // setter
  set writer(updatedAuthor) {
    this._author = updatedAuthor;
  }
}
const novel = new Book('anonymous');
console.log(novel.writer);  // anonymous
novel.writer = 'newAuthor';
console.log(novel.writer);  // newAuthor
Posted by: Guest on January-12-2021
2

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);
Posted by: Guest on September-14-2020
-1

getter and setter in javascript

const person = {
    name: "abc",
  
    get Name() {
      return this.name;
    },
  
    set Name(v) {
      this.name = v;
    }
  };
  
 console.log(person.Name); 
 person.Name = 'John Doe'; 
 console.log(person.Name);
Posted by: Guest on August-17-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language