JS interception method

About JS interception method

I have been looking for articles about JS interception methods on the Internet for a long time, but I have not found anything. After careful study, I sorted out the following code, and I hope you can correct me a lot.
In fact, the most important 
Object.getOwnPropertyNames(example).forEach(function (property)
This code traverses all the methods inside the object, and judges that if it is a method and is equal to the method name, intercept it. Here I intercept all the methods of the window in the world, but it doesn't feel very good. method to filter out its own methods in the window object.
original.apply(example, Array.prototype.slice.call(arguments));
The meaning of this code is to add the original method back. If it is removed, the code in the original method will not be executed, and the method code passed by interception will be executed.

<span style="font-family: Arial, Helvetica, sans-serif;">intercept(window,"hello",fn);</span>
  1. function intercept ( example, funName, fun ){ //example traverses objects, the name of the method that funName needs to intercept, and the method to be executed after fun intercepts
  2. Object.getOwnPropertyNames(example).forEach(function (property) {
  3. var original = example[property];
  4. if (typeof original === "function"&&original.name == funName) {
  5. example[property] = function () {
  6. if(fun){
  7. fun(property);
  8. }else{
  9. return false;
  10. }
  11. original.apply(example, Array.prototype.slice.call(arguments));
  12. }
  13. }
  14. });
  15. }
  16. function fn(property){
  17. console.log('Intercepted call to function:', property);
  18. }
  19. function hello(varstr){
  20. console.log('varstr=',varstr);
  21. }
  22. hello('test');


Related: JS interception method