Hello,

function declaration via let / const / var - should we strive for this?

If you declare functions in this way, you have to swap the blocks, and the result is not very readable. Code example:

-How to re-declare f-tion to be beautiful?

// замСняСт всС <div class="include">html_link</div> Π½Π° содСрТимоС скачанноС ΠΏΠΎ ссылкС html_link function newInclude() { // собираСм всС ΠΈΠ½ΠΊΠ»ΡƒΠ΄Ρ‹ const includes = [].slice.call(document.getElementsByClassName('include')); for (const include of includes) { const url = include.innerHTML; uploadUrl(include, url); } // скачиваСм содСрТимоС с url function uploadUrl(include, url) { fetch(url) .then(function(response) { return response.text(); }) .then(function(responseText) { insertInclude(include, responseText); }); } // ставим содСрТимоС url вмСсто Π±Π»ΠΎΠΊΠ° include function insertInclude(include, responseText) { include.insertAdjacentHTML('afterend', responseText); include.remove(); } } 
 <div>some html...</div> <div class="include">top/top.html</div> <div>some html...</div> 

  • one
    You would give an example of code in which "everything breaks". - Igor
  • one
    functions are usually declared without let / const / var)) - Alexey Shimansky
  • @Igor, sorry for my explanations. I meant "beautiful clear house breaks", and the code works - Stanislav Sagan
  • Without sample code is really incomprehensible. I can write the answer, but not sure if it will address the problem. - etki
  • We strive to understand why let and const were introduced, as well as the differences between the functional expression and the declaration function and not to write nested functions. - user220409

1 answer 1

Do you mean this kind of function?

 const r = function () {}; const b = () => {}; 

Functions of this kind are called functional expressions, its main difference from ordinary functions is that the interpreter reads the normal functions immediately, stores them in memory, and then, by necessity, calls, but such functions have global visibility, whereas functional expressions do not have global visibility and they are read only in the course of reading the code by the interpreter. Those. they are only visible when the interpreter reads them.

On this subject there is an article in the textbook in which everything is described in detail.

  • Thank you very much, but what is actually the convenience of this? At the top of the function, at the bottom, the code is not wrapped up in the function .. Or is it necessary to arrange the code differently? whereas ? - Stanislav Sagan
  • This has a practical meaning, and not in the design of the code. Using a functional expression, for example, allows you to create different functions from certain conditions, but the name of the function will be one. - DimenSi
  • I still found the point - rename all anonymous functions. True, if the function has this - the problem turns out - Stanislav Sagan