Skip to content

Instantly share code, notes, and snippets.

@interjc
Created December 30, 2015 07:30
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 interjc/18e40f240d647914fac7 to your computer and use it in GitHub Desktop.
Save interjc/18e40f240d647914fac7 to your computer and use it in GitHub Desktop.
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