1

I am creating a web app in MVC with Javascript in which i have a function which look like the following

function test() {
   //this function won't do anything but create a new function inside.
     function executeLot(lot1, lot2) {
       //normal function execution;
     }
}

now i want to call the function executeLot(1,2) but i am not able to call this as it is located inside test()

what can i do to call executeLot from outside of the test function.

2
  • You can easily implement in this way. function test() { //this function won't do anything but create a new function inside. executeLot(lot1, lot2); } function executeLot(lot1, lot2) { //normal function execution; } Commented Jun 18, 2018 at 11:19
  • Give more epxlanation! Did you use this function only like simple method ? Commented Jun 18, 2018 at 11:21

3 Answers 3

1

Best way for your MVC platform is class model based system not global methods or procedurally code.

See example :

//////////////////////////////////////////////////////////
// Class Definition ECMA 5 - works on all modern browsers 
//////////////////////////////////////////////////////////

function Test() {

     this.executeLot = function(lot1, lot2) {
       //normal function execution;
       console.log(lot1 + " <> " + lot2)
     }
     
}

//////////////////////////////////
// Make instance from this class
//////////////////////////////////

var myTest = new Test();

//////////////////////////////////
// Call method
//////////////////////////////////
myTest.executeLot(1,1);

Sign up to request clarification or add additional context in comments.

Comments

0

You could return a function and assign it into a variable like this:

function test(){
    return function(arg1,arg2){
        // do your magic here
    }
}

var executeLoot = test();
//Call your returned function
var arg1 = 1;
var arg2 = 2;
executeLoot(arg1,arg2);

Comments

0

You can't call the function directly. You will have to return it like this:

function test() {
  return function executeLot(lot1, lot2) {
    // [...]
  }
}

6 Comments

for God's shake NO NO NO
@ManosKounelakis I'm assuming you are talking about the latter version? Yes it is rather ugly and I should probably remove it. Or are you referring to something else?
Yeah I am talking about the latest version
inside test you assign executeLoot to myFunction
check again the body of the test function in your 2nd code
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.