Skip to content

Instantly share code, notes, and snippets.

@kichMan
Last active January 12, 2016 17:13
Show Gist options
  • Save kichMan/20f5dfca07a23f90b8c9 to your computer and use it in GitHub Desktop.
Save kichMan/20f5dfca07a23f90b8c9 to your computer and use it in GitHub Desktop.
Add accessor for native method
/**
* @description Особенность данного метода в том, что позволяет повесить акцессор
* на метод без создания нового события (Event) и слушателя
* к нему (addEventListener)
*
* При этом, выгода заключается в сохранении метода,
* ранее назначенного через свойство.
*/
(function(){
var fn = document.onclick;
document.onclick = function(){
if(fn){
fn.call(this, arguments);
}
console.log('It is here call accessor');
};
/* Do not init "get" method, because this method has native init */
Object.defineProperty(document, 'onclick', {
set: function(_fn){ fn = _fn; }
});
})();
document.onclick = function(){
console.log('set global onclick');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment