5

I have been reading "javascript: the good part".

Function.prototype.method = function (name, func) {
     this.prototype[name] = func;
     return this;
};

Example usage is:

Number.method('integer', function () {
    return Math[this < 0 ? 'ceiling' : 'floor'](this);
});
document.writeln((-10 / 3).integer()); // -3

Two questions:

  1. "By augmenting Function.prototype with a method method, we no longer have to type the name of the prototype property. That bit of ugliness can now be hidden." What does that mean? So it saves typing ".prototype.integer"? Doesn't seem to be super important.

  2. We augmented Function.prototype, which sounds it's specific to functions. Number is a native type, should we have augmented Object.prototype instead?

4

2 に答える 2