There is something like this:

var request = new function(){ var jsonAjaxResult, countOfDownloadingArts; function downloadArts(){ return Promise((resolve, reject) => { // Код для загрузки картинок }); } function downloadJSON(){ return Promise((resolve, reject) => { // Код для загрузки json-а }); } this.getNewArts = (numberOfShowingArts = 20) => { // Обработчик очереди возвращающихся промисов для загрузки // заданного количества картинок (формальный параметр) } } 

In it, I use methods like this:

 request.getNewArts(40); 

Very convenient.
But when I write this kind of construct, jsLint swears this error:

 Weird construction. Is 'new' necessary? 

Well, I'm wondering why I can't use an anonymous function as a constructor? As I understand it, this is simply ugly from the point of view of logic, but I really need one copy of this functionality.
How then to be? To make a mistake or can I somehow rewrite the code?
Honestly, I would like it to be like this:

 function request(){ // функции и переменные } // И использовать вот так: request.getNewArts(40); 

But alas, in js as impossible, as I understand it.

  • Comments are not intended for extended discussion; conversation moved to chat . - Nick Volynkin

1 answer 1

The new operator sets this inside a function to a newly created object. Thus, if this not used inside a function, this may serve as a signal that new may not be needed here either.

Judging by the code in the question: the getNewArts method is added to the object being created, hence the use of new is fully justified.

On the other hand, since it is not entirely trivial to create some more objects of this type, the constructor call can be replaced with a normal function call that returns an object:

 var request = function(){ var jsonAjaxResult, countOfDownloadingArts; function downloadArts(){ return Promise((resolve, reject) => { // Код для загрузки картинок }); } function downloadJSON(){ return Promise((resolve, reject) => { // Код для загрузки json-а }); } return { getNewArts : (numberOfShowingArts = 20) => { // Обработчик очереди возвращающихся промисов для загрузки // заданного количества картинок (формальный параметр) } } }(); 

In this case, the new operator is not needed.