Skip to content

Instantly share code, notes, and snippets.

@bangedorrunt
Last active August 29, 2015 14:25
Show Gist options
  • Save bangedorrunt/7951ef720a0551f1484e to your computer and use it in GitHub Desktop.
Save bangedorrunt/7951ef720a0551f1484e to your computer and use it in GitHub Desktop.
Mixin Patterns
function delegate (receiver, methods, toProvider) {
methods.forEach(function (methodName) {
receiver[methodName] = function () {
return toProvider[methodName].apply(receiver, arguments);
};
});
return receiver;
}
var portfolio = {
addInvestment(investment) {
this._investments.push(investment);
return this;
},
netWorth() {
return this._investments.reduce(
function (acc, investment) {
return acc + investment;
},
0
);
}
};
var investor = {
_investments: []
// ..
};
delegate(investor, ['addInvestment', 'netWorth'], portfolio);
investor.addInvestment(7);
console.log(investor.netWorth()); // => net worth: 7
function delegateToOwn (receiver, methods, propertyName) {
methods.forEach(function (methodName) {
receiver[methodName] = function () {
var toProvider = receiver[propertyName];
return toProvider[methodName].apply(receiver, arguments);
};
});
return receiver;
};
function delegate (receiver, methods, toProvider) {
methods.forEach(function (methodName) {
receiver[methodName] = function () {
return toProvider[methodName].apply(receiver, arguments);
};
});
return receiver;
};
let person = {
fullName() {
return this.firstName + " " + this.lastName;
},
rename(first, last) {
this.firstName = first;
this.lastName = last;
return this;
}
}
let sam = {
firstName: 'Sam',
lastName: 'Lowry',
assistant: person
};
delegateToOwn(sam, ['fullName', 'rename'], 'assistant');
console.log(sam.fullName());
let new_person = {
fullName() {
return "Hey, don't want to be your assistant!!!";
}
}
sam.assistant = new_person;
console.log(sam.fullName()); // => Hey, don't want to be your assistant!!!
let __slice = [].slice;
function extend () {
let consumer = arguments[0],
providers = __slice.call(arguments, 1),
key,
i,
provider;
for (i = 0; i < providers.length; ++i) {
provider = providers[i];
for (key in provider) {
if (provider.hasOwnProperty(key)) {
consumer[key] = provider[key];
};
};
};
return consumer;
};
let sam = {
firstName: 'Sam',
lastName: 'Lowry',
};
let person = {
fullName() {
return this.firstName + " " + this.lastName;
},
rename(first, last) {
this.firstName = first;
this.lastName = last;
return this;
}
}
extend(sam, person);
function extendPrivately (receiver, template) {
let methodName,
privateProperty = Object.create(null);
for (methodName in template) {
if (template.hasOwnProperty(methodName)) {
receiver[methodName] = template[methodName].bind(privateProperty);
};
};
return receiver;
};
let sam = {
firstName: 'Sam',
lastName: 'Lowry',
};
let person = {
fullName() {
return this.firstName + " " + this.lastName;
},
rename(first, last) {
this.firstName = first;
this.lastName = last;
return this;
}
}
extendPrivately(sam, person);
function forward (receiver, methods, toProvider) {
methods.forEach(function (methodName) {
receiver[methodName] = function () {
return toProvider[methodName].apply(toProvider, arguments);
};
});
return receiver;
};
let portfolio = {
_investments: [],
addInvestment(investment) {
this._investments.push(investment);
return this;
},
netWorth() {
return this._investments.reduce(
function (acc, investment) {
return acc + investment;
},
0
);
}
};
let investor = {
// ..
};
forward(investor, ['addInvestment', 'netWorth'], portfolio);
investor.addInvestment(7);
console.log('net worth: ' + investor.netWorth()); // => net worth: 7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment