I am using jQuery and I am a beginner. I have this structure:
function a(){
function b(){
return x;
}
}
I want to return x from primary function that is a(). How can I do this?
You have a few options.
Assuming:
var x = 1;
You could do this:
// Return a function when calling `a()`, which can be called --> `a()()`;
function a() {
return function b (){
return x;
}
}
a()(); // 1;
// Return the result of `b()` when calling `a()`;
function a(){
function b(){
return x;
}
return b();
}
a(); // 1;
// Return a object containing a function named `b`.
function a(){
return {
b: function(){
return x;
}
};
}
a().b(); // 1;
Try this fiddle:
function a() {
var x = 34;
return function b (){
return x;
}
}
var f1 = a();
var f2 = f1();
alert(f2); //34
b()ina's body.