Subash A
Subash A

Reputation: 33

How to call a function inside a main function in javascript

function chart(){
       Barchart();
       Linechart();
       function Barchart(){
       }
       function Linechart(){
       }
}

Chart() is main parent function. Inside I have two chart functions. I have to call only line chart. Chart(); will execute both barchart and linechart. but I need to call only line chart.

Upvotes: 2

Views: 566

Answers (8)

Dave
Dave

Reputation: 3091

i hope these will be helpful.

function chart() {
function Barchart()
{
    alert('Barchart');
}
function Linechart() 
{
    alert('Linechart');
}
Barchart();  
Linechart();
}
chart();

Upvotes: 0

Jason Graham
Jason Graham

Reputation: 904

Although less common, nesting functions this way is possible. It's a method called currying. Your function Linechart() is scoped within chart(), so at the moment, it can only be called there. In order to make it available elsewhere, you'll need to place it out with the function so the scope is broadened. Something along the lines of:

   function Linechart(){
     alert("Line called!");
   }

function chart(){

   function Barchart(){
     alert("Bar called!");
   }

    Barchart();
    Linechart();
}

chart(); 
// Line chart on it's own
Linechart();

This will mean that when you call chart(), both Linechart() and Barchart() are called, but you'll also be able to call Linechart() on its own.

Upvotes: 0

DK3
DK3

Reputation: 403

you should do this so that you have good control on your function calling or you directly Linechart also return instead of init

var chart = (function () {

        var Barchart = function () {
            console.log("bar chart");
        };
        var Linechart = function () {
            console.log("line chart");
        };
        var init = function () {
            Linechart();
        }
        return {
            init: init
            //you can retrun Barchart or Linechart which you want to access publically
        }
    })();
    chart.init();

Upvotes: 1

Lux
Lux

Reputation: 18240

You can't!

The scope of this functions is only the chart() function, so they are not accessible outside, unless you give them out from inside chart() somehow, like by returning them.

Upvotes: 0

nimig
nimig

Reputation: 154

after your edit just remove the first call to Barchart()

Upvotes: 0

Rohith Nair
Rohith Nair

Reputation: 29

If you want the inner function to be executed,

function outer() { 
    function inner() {
        alert("hi");
    }
    inner(); // call it
}

Hope this helps.

Upvotes: 1

Jack jdeoel
Jack jdeoel

Reputation: 4584

I know only object function can call for one specific function ..

var chart = {
       Barchart : function(){
        alert('');
       },
       Linechart : function(){
        alert('');
       }
}
chart.Barchart();

Upvotes: 2

Bryon Mulligan
Bryon Mulligan

Reputation: 46

Yes, this is true. Both will execute in the order given. You need to provide some sort of selection. Either using an if/then or some other way.

If(myChecbox.isSelected){
barchart();}
else{
linechart();
}

Ask yourself, what is it that is making the choice? Let me know what you come up with. You will need to place the called functions outside of your method...as stated in the others post.

Upvotes: 2

Related Questions