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