Created
August 24, 2016 14:18
-
-
Save iuliaL/d6b013b43f139535416c923b31323909 to your computer and use it in GitHub Desktop.
Getters and Setters in ES6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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