Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 443
John Avatar asked Oct 19 '10 09:10

John


People also ask

What are the parts of JavaScript?

The core ecosystem of JavaScript consists of 3 main components which are ECMAScript, JavaScript Engine, and the JavaScript Runtime.

What is method in JavaScript programming?

JavaScript methods are actions that can be performed on objects. A JavaScript method is a property containing a function definition. Property. Value.

Is JavaScript a Good language?

Whether you're planning on eventually being a front-end or back-end developer, there's no doubt that JavaScript is the best coding language to learn for beginners.


2 Answers

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.

like image 183
Tim Down Avatar answered Sep 20 '22 13:09

Tim Down


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.

like image 23
Jonathan Avatar answered Sep 17 '22 13:09

Jonathan