Reputation:
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.
Upvotes: 1
Views: 146
Reputation: 4244
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);
Upvotes: 1
Reputation: 589
You can't call the function directly. You will have to return it like this:
function test() {
return function executeLot(lot1, lot2) {
// [...]
}
}
Upvotes: 0
Reputation: 3161
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);
Upvotes: 0