Function definition inside function

If I have a code:

function A() { function B() { } B(); } A(); A(); 

- function B is parsed and created every time I call A (so that it can reduce the performance of A)?

+6
source share
2 answers

If you want to use the function only internally, how about closing. Here is an example

  var A = (function () { var publicFun = function () { console.log("I'm public"); } var privateFun2 = function () { console.log("I'm private"); } console.log("call from the inside"); publicFun(); privateFun2(); return { publicFun: publicFun } })(); console.log("call from the outside"); A.publicFun(); A.privateFun(); //error, because this function unavailable 
+2
source
 function A(){ function B(){ } var F=function(){ B(); } return F; } var X=A(); //Now when u want to use this just use this X function it will work without parsing B() 
+2
source

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


All Articles