“method” method in Crockford's book: [removed]

2019-01-13 06:50发布

问题:

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?

回答1:

All native functions in JavaScript inherit from Function.prototype. Number, String, Object, Array and RegExp are all functions, therefore they inherit from Function.prototype.

method is intended to be called on constructor functions. Its job is to make the function you supply to it into a method that exists for every object created by the constructor function on which you called method. You will notice that in the functions that Crockford passes to method, he makes use of this, which is a reference to the object on which the method was called. Array.dim, Array.matrix and Array.identity make no use of this because they operate independently of any particular array and hence do not need to be methods of individual array objects. They are assigned as properties of the Array function for convenience: they could equally well exist on their own as functions in the global scope.



回答2:

As an aside, on P40:

The end () means "use the function that this function returns", not the outer function that returns it.

If he had left off the final (), a call to deentityify would return a function, rather than a string.

In Douglas Crockford's own words:

We immediately invoke the function we just made with the () operator. That invocation creates and returns the function that becomes the deentityify method.



回答3:

The solution as given by @Tim Down is accurate, but not completely clear.

Function object vs Function instance object

First of all, in javascript, a function is also an object. From this, I mean not the object created by new () construct, but the function itself. To avoid confusion, I would refer such objects as Function object, and for object created using new () construct of a function as Function instance object.

_ proto _ and prototype properties

Any function object in javascript has two properties: _ proto _ and prototype. Moreover, any Function instance object (created using new constructor) has a property _ proto _ . The _ proto _ is what defines the inheritance. Some good resource on this could be found at

http://zeekat.nl/articles/constructors-considered-mildly-confusing.html

How is inheritance defined?

An object objA inherits another object objC if objA and objC are connected through any number of _ proto _ . So if objA has _ proto _ equal to objB, and objB has _ proto _ equal to objC, then objA inherits objB and objC, while objB inherits objC.

What is meant by inheritance?

It means any inheriting object can use any property of inherited object.

What is Function.prototype

It is the object whom _ proto _ of every function object refers. This means every Function object has access to properties of Function.prototype, since every Function object inherits Function.prototype object. This also means that if method property is added to Function.prototype object, it would be available to all the possible Function objects in javascript. This includes Strings, Number, etc.

this.prototype[name] = func;

this refers to Function object when the 'method' is invoked from Function objects like Number, String, etc. Which means that we now have a new property in Function object with name "name", and its a function 'func'.

What good is prototype property of Function object

A function object's prototype is referred to by the Function instance object's _ proto _ created using that function's new construct.

If the following was executed:

Number.method('integer', function () {...});

then Number.prototype has that integer method defined in it. This means each Number function instance object, e.g. new Number (2.4), would "inherit" this new property 'integer' from Number.prototype, since that Number function instance object would have its _ proto _ set to Number.prototype.



回答4:

Try to use this prototype method:

String.prototype.deentityify = function () { ... }

Then:

document.writeln('<">'.deentityify( ));

We can see: <">



回答5:

Example: Currying can be rewritten as follows, if anyone got stuck. See jsFiddle demo

 Function.prototype.curry = function ( ) {
 var slice = Array.prototype.slice;
 var args = slice.apply(arguments);
 var that = this; 
 return function () {  
    return that.apply(null,args.concat (slice.apply(arguments)));
 }
};
var add = function(a, b)
{
    return a + b;
}
var add1 = add.curry(1);
document.writeln(add1(6)); // 7