Here are two widely seen code snippets in JavaScript:
Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
};
Number.method('integer', function () {
return Math[this < 0 ? 'ceil' : 'floor'](this);
});
Obviously, the this
in the second snippet stands for the Number object that's calling the augmented integer
method. How about the this
in the first snippet? From the prototype
property we can guess it stands for the constructor that's being augmented, but the logic behind the snippet is elusive to me. Can anyone detail an explanation? Thanks.