Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function definition inside function [duplicate]

Tags:

javascript

If i have code:

function A() {

  function B() {

  }

  B();

}

A();
A();

is B function parsed and created each time i call A(so it can decrease performance of A)?

like image 482
Krab Avatar asked Jul 16 '13 07:07

Krab


People also ask

What is duplicate function declaration?

Duplicate declarations are simply an equivalent restatement of an interface specification, and that has no effect on code which depends on that interface.

Can you nest functions in JavaScript?

JavaScript allows for the nesting of functions and grants the inner function full access to all the variables and functions defined inside the outer function (and all other variables and functions that the outer function has access to).

What does function() mean in JavaScript?

In JavaScript, a function allows you to define a block of code, give it a name and then execute it as many times as you want. A JavaScript function can be defined using function keyword.

What is function declaration in JavaScript?

The function declaration (function statement) defines a function with the specified parameters. You can also define functions using the Function constructor and a function expression.


1 Answers

If you want use a function only internally, how about closure. Here 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
like image 83
IgorCh Avatar answered Oct 13 '22 07:10

IgorCh