Skip to content

Instantly share code, notes, and snippets.

@lucassus
Last active August 26, 2015 14:32
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 lucassus/61025de7beb41ccc7e3d to your computer and use it in GitHub Desktop.
Save lucassus/61025de7beb41ccc7e3d to your computer and use it in GitHub Desktop.
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$$$!""
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