Advertisement
Guest User

Untitled

a guest
Jul 29th, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  1. // Also works for normal methods
  2. class User {
  3. constructor (first, last) {
  4. this.firstName = first;
  5. this.lastName = last;
  6. }
  7. }
  8.  
  9. Object.defineProperty(User.prototype, 'name', {
  10. get: function () {
  11. return `${this.firstName} ${this.lastName}`
  12. },
  13. set: function (name) {
  14. let split = name.split(' ');
  15. this.firstName = split[0];
  16. this.lastName = split[1];
  17. }
  18. });
  19.  
  20. let user = new User('jon', 'abrams');
  21. console.log(user.name); // outputs "jon abrams"
  22.  
  23. user.name = "Jon Abrams";
  24. console.log(user.name); // outputs "Jon Abrams"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement