JavaScript: optional types. The goal is to get it back.

Now I am reading JavaScript - The Good Parts. Therefore, I am dealing with an increase in types. I understand motivation and implementation. But if I look at the code ...

Function.prototype.method = function(ident, funct) { this.prototype[ident] = funct; return this; // No idea. For what? }; 

... then I do not understand the purpose of the return. I returned the comments in the comments. It does not affect. It functioned anyway.

My full code is:

 Function.prototype.method = function(ident, funct) { this.prototype[ident] = funct; return this; }; Date.method('sayHello', function() { alert(new Date().toString()); }); var myDate = new Date(); myDate.sayHello(); 

What is this for?

+6
source share
1 answer

This is usually done so that you can catch method calls, the so-called "free interfaces":

 obj.method().anotherMethod().yetAnotherMethod() 

eg:.

 'string'.toUpperCase().substr(2).repeat(3) 

In the case of a string, this returned instead of a new string, but you understand why it is useful.

+14
source

Source: https://habr.com/ru/post/985248/


All Articles