0

Section 6.6. of the book 'JavaScript: The Good Parts', calls a method of Array as follows. Where in the prototypical inheritance hierarchy of JavaScript objects is the 'method' method defined. I have searched the annotated ECMAScript 5.1 reference on the Mozilla Developer Network JavaScript documentation but could not find it.

Array.method('reduce', function (f, value) {
    var i;
    for (i = 0; i < this.length; i += 1) {
        value = f(this[i], value);
    }
    return value;
});

If anyone could tell me where this method is coming from I would greatly appreciate it.

Thanks.

John Sonderson
  • 3,238
  • 6
  • 31
  • 45
  • 1
    I imagine it's a custom method implemented to take a function name and implementation. If the native function doesn't exist, it'll implement the polyfill for you. Only an educated guess though... – benhowdle89 Nov 03 '14 at 16:27
  • Crockford is not the best place to go to for "classical inheritance" he never does it right. Creates an instance of Parent to set prototype of Child and claimed Patent constructor can't be re used. He's worried about encapsulation and needs private members but breaks it when changing objects he doesn't own. More on prototype in this answer: http://stackoverflow.com/questions/16063394/prototypical-inheritance-writing-up/16063711#16063711 – HMR Nov 03 '14 at 23:30
  • 1
    Even if you don't like constructor functions then the following eBook has better examples. I suggest reading the answer in the other comment first so you understand instance specific and prototype members and know the role a constructor plays. Then this one: https://github.com/getify/You-Dont-Know-JS/tree/master/this%20%26%20object%20prototypes – HMR Nov 03 '14 at 23:34

1 Answers1

1

Crockford adds that method to Function.prototype as a helper (like benhowdle89 suggested):

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

"method" method in Crockford's book: Javascript: The Good Parts

Community
  • 1
  • 1
pherris
  • 17,195
  • 8
  • 42
  • 58