Douglas Crockford wrote in his book (Page 4):
Throughout the book, a method
method is used to define new methods, This is its definition:
Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
};
Then he starts to use this method
to add method in Number, String, Function, Object, Array, RegExp
, and here is the complete list:
P33:
Number.method('integer', function () {...});
String.method('trim', function () {...});
P40 (not sure if there is a misprint in Page 41: the end () ):
String.method('deentityify', function () {...}());
P43 & P44:
Function.method('curry', function () {...});
P47 (I am confused here, don't know why Crockford define new
method, and he seems never use new
method in the book):
Function.method('new', function () {...});
P48:
Function.method('inherits', function (Parent) {...});
P54:
Object.method('superior', function (name) {...});
P62:
Array.method('reduce', function (f, value) {...});
P79:
Array.method('pop', function () {...});
Array.method('push', function () {...});
Array.method('shift', function () {...});
P82:
Array.method('splice', function (start, deleteCount) {...});
P84:
Function.method('bind', function (that) {...});
P88:
RegExp.method('test', function (string) {...});
String.method('charAt', function (pos) {...});
P90 (not sure if there is a misprint in Page 91: the end () ):
String.method('entityify', function () {...}());
The definition method
is based on Function
, why it can be used in Number, String, Object, Array, RegExp
besides Function
? And can this method
be used to other data type?
Another small question: in Page 63 & 64, the definition of Array.dim, Array.matrix, Array.identity
didn't use above method
, why?
See Question&Answers more detail:
os