Last active
August 26, 2015 14:32
-
-
Save lucassus/61025de7beb41ccc7e3d to your computer and use it in GitHub Desktop.
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
function bang(target, name, descriptor) { | |
var _get = descriptor.get; | |
descriptor.get = function() { | |
return _get.call(this) + '!'; | |
} | |
} | |
function wrap(pattern) { | |
return function (target, name, descriptor) { | |
var _get = descriptor.get; | |
descriptor.get = function() { | |
return pattern + _get.call(this) + pattern; | |
} | |
} | |
} | |
class User { | |
constructor({ firstName, lastName }) { | |
this.firstName = firstName; | |
this.lastName = lastName; | |
} | |
@bang | |
@wrap('$$$') | |
get fullName() { | |
return `${this.firstName} ${this.lastName}`; | |
} | |
} | |
const user = new User({ firstName: 'Luke', lastName: 'Skywalker' }); | |
console.log(user.fullName); // => "$$$Luke Skywalker$$$!"" |
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
function bang(target, name, descriptor) { | |
var _get = descriptor.get; | |
descriptor.get = function() { | |
return _get.call(this) + '!'; | |
} | |
} | |
function wrap(pattern) { | |
return function (target, name, descriptor) { | |
var _get = descriptor.get; | |
descriptor.get = function() { | |
return pattern + _get.call(this) + pattern; | |
} | |
} | |
} | |
function mixin(object) { | |
return function(target) { | |
Object.assign(target.prototype, object); | |
} | |
} | |
const greeter = { | |
sayHello: function(name) { | |
return this.fullName + ' says hello to ' + name; | |
} | |
} | |
@mixin(greeter) | |
class User { | |
constructor({ firstName, lastName }) { | |
this.firstName = firstName; | |
this.lastName = lastName; | |
} | |
@bang | |
@wrap('$$$') | |
get fullName() { | |
return `${this.firstName} ${this.lastName}`; | |
} | |
} | |
// Object.defineProperty(User.prototype, 'fullName', { | |
// get: function() { return `${this.firstName} ${this.lastName}` } | |
// }); | |
const user = new User({ firstName: 'Luke', lastName: 'Skywalker' }); | |
console.log(user.fullName); | |
console.log(user.sayHello('Vader')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment