Last active
January 12, 2016 17:13
-
-
Save kichMan/20f5dfca07a23f90b8c9 to your computer and use it in GitHub Desktop.
Add accessor for native method
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
/** | |
* @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