var UserMock = (function() { var User; User = function() {}; User.prototype.isValid = function() {}; return User; })();
Just through prototype :
(function(_old) { UserMock.prototype.isValid = function() {
Explanation:
(function(_old) {
and
})(UserMock.prototype.isValid);
Makes a reference to the isValue method on the isValue variable. Closing is done so that we do not change the parent area with the variable.
UserMock.prototype.isValid = function() {
Updates the prototype method.
return _old.apply(this, arguments);
Call the old method and return the result from it.
Using apply allows you to put in the desired area ( this ) all the arguments passed to the function. For example. if we make a simple function and apply it.
function a(a, b, c) { console.log(this, a, b, c); }
source share