Answers for "javascript class getter setter"

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 class setter

const language = {
  set current(name) {
    this.log.push(name);
  },
  log: []
}

language.current = 'EN';
language.current = 'FA';

console.log(language.log);
Posted by: Guest on March-13-2020
0

javascript class getter setter

class Porn_Stars{
                   constructor(name,age){
                              this.name = name;
                              this.age = age
                   };
                        get get_data () {
                              return this.name;
                        };
                        set change_data (value) {
                              this.name = value;
                        }

             };
              const data = new Porn_Stars("Chloe Cherry",21);
                   console.log("This is the name before she has changed > "+data.name+"<br/>");
                        data.change_data="Chloe Culture";
                   console.log("After changing the name,"+data.name)
Posted by: Guest on August-17-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language