Skip to content

Instantly share code, notes, and snippets.

@iuliaL
Created August 24, 2016 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iuliaL/d6b013b43f139535416c923b31323909 to your computer and use it in GitHub Desktop.
Save iuliaL/d6b013b43f139535416c923b31323909 to your computer and use it in GitHub Desktop.
Getters and Setters in ES6
'use strict';
class Student {
constructor({ firstName, lastName, age, interestLevel = 5 } = {}) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.interestLevel = interestLevel;
}
get name(){
return `${this.firstName} ${this.lastName}`;
}
set name(input) {
let chunks = input.split(' ');
this.firstName = chunks[0];
this.lastName = chunks[1];
}
};
let iulia = new Student({firstName: 'Iulia Maria', lastName: 'Lungu', age:26 });
//just getter here
console.log(iulia.name);
//setter here
iulia.name = 'Iulia Samson';
console.log(iulia.name);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment