Can anyone explain this code to me? Whats the meaning and how to use it?
Function.prototype.createInterceptor = function createInterceptor(fn) {
var scope = {};
return function () {
if (fn.apply(scope, arguments)) {
return this.apply(scope, arguments);
}
else {
return null;
}
};
};
var interceptMe = function cube(x) {
console.info(x);
return Math.pow(x, 3);
};
//
var cube = interceptMe.createInterceptor(function (x) {
return typeof x === "number";
});
The code is not functional as is, so I made this edit:
The interceptor function validates what is passed to the original function before calling it, and returns
nullif it didn’t see the arguments as valid. If the arguments are valid, then the original function
is called.