Skip to content

Instantly share code, notes, and snippets.

@interjc interjc/AOP.markdown
Created Dec 30, 2015

Embed
What would you like to do?
AOP
<div id="result"></div>
window.addEventListener('DOMContentLoaded', function(){
var appendBefore = function(){
appendText('before');
},
appendAfter = function(){
appendText('after');
},
newAbc = abc
.before(appendBefore)
.after(appendAfter)
.after(appendAfter)
.before(appendBefore);
newAbc();
});
Function.prototype.before = function(fn){
var self = this;
return function(){
typeof fn === 'function' && fn.apply(this, arguments);
return self.apply(this, arguments);
};
};
Function.prototype.after = function(fn){
var self = this;
return function(){
var ret = self.apply(this, arguments);
typeof fn === 'function' && fn.apply(this, arguments);
return ret;
}
};
var appendText = function(text){
var obj = document.querySelector('#result'),
str = document.createTextNode(' '+ text +' ');
obj.appendChild(str);
},
abc = function(){
appendText('raw');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.