Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Also works for normal methods
- class User {
- constructor (first, last) {
- this.firstName = first;
- this.lastName = last;
- }
- }
- Object.defineProperty(User.prototype, 'name', {
- get: function () {
- return `${this.firstName} ${this.lastName}`
- },
- set: function (name) {
- let split = name.split(' ');
- this.firstName = split[0];
- this.lastName = split[1];
- }
- });
- let user = new User('jon', 'abrams');
- console.log(user.name); // outputs "jon abrams"
- user.name = "Jon Abrams";
- console.log(user.name); // outputs "Jon Abrams"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement