简体   繁体   中英

“method” method in Crockford's book: Javascript: The Good Parts

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?

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.

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.

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

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 , and for object created using new () construct of a function as . ,并将使用new()构造函数创建的对象称为

Any function object in javascript has two properties: and . Moreover, any Function instance object (created using new constructor) has a property . The is what defines the inheritance. 是定义继承的原因。 Some good resource on this could be found at

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

An object objA inherits another object objC if objA and objC are connected through any number of . 连接,则对象objA继承另一个对象objC。 So if objA has equal to objB, and objB has equal to objC, then objA inherits objB and objC, while objB inherits objC. 等于objB,并且objB具有等于objC,则objA继承objB和objC,而objB继承objC。

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

It is the object whom 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 object s like Number, String, etc. Which means that we now have a new property in Function object with name "name", and its a function 'func'.

A function object 's is referred to by the Function instance object 's created using that function's new construct. 由使用该函数的新构造创建的Function实例对象 引用

If the following was executed:

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

then Number.prototype has that method defined in it. 方法。 This means each Number function instance object , eg new Number (2.4), would "inherit" this new property 'integer' from Number.prototype, since that Number function instance object would have its set to Number.prototype. 设置为Number.prototype。

Try to use this prototype method:

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

Then:

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

We can see: <">

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM