I am trying to call the nested function is not working
Here is what I tried jsfiddle
Script:
(function( $ ){
$.fn.investPage = function() {
function setupFCConfig(){
$('.nestedFunction').click(function(){
alert('setupFCConfig func()');
});
}
$(".edit").on('click', function(){
alert('edit click func()');
});
$(".cancel").on('click', function(){
alert('cancel click func()');
});
$(".checkout").click(function(){
alert('checkout click func()');
});
};
})( jQuery );
$.fn.investPage();
$.fn.investPage.setupFCConfig();
You can call it using function2() in ES6 .
A function defined inside another function is called a nested function. Nested functions can access variables of the enclosing scope. In Python, these non-local variables are read-only by default and we must declare them explicitly as non-local (using nonlocal keyword) in order to modify them.
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).
setupFCConfig()
is NOT a property of the $.fn.investPage object so it can't be called like this:
$.fn.investPage.setupFCConfig();
It is a local function that is not available outside the scope in which it is declared. If you want it available from an outside scope, then you need to assign it to be a property of some object that is available in that outside scope.
For example, you could change the definition of that function to be like this:
(function( $ ){
$.fn.investPage = function() {
$(".edit").on('click', function(){
alert('edit click func()');
});
$(".cancel").on('click', function(){
alert('cancel click func()');
});
$(".checkout").click(function(){
alert('checkout click func()');
});
};
$.fn.investPage.setupFCConfig = function (){
$('.nestedFunction').click(function(){
alert('setupFCConfig func()');
});
}
})( jQuery );
$.fn.investPage();
$.fn.investPage.setupFCConfig();
FYI, you also need to fix the misspelling of .click
.
you are using the wrong scope for the function
(function( $ ){
$.fn.investPage = function() {
$(".edit").on('click', function(){
alert('edit click func()');
});
$(".cancel").on('click', function(){
alert('cancel click func()');
});
$(".checkout").click(function(){
alert('checkout click func()');
});
};
$.fn.investPage.setupFCConfig = function(){
$('.nestedFunction').click(function(){
alert('setupFCConfig func()');
});
};
})( jQuery );
JSFiddle
or
(function( $ ){
$.fn.investPage = function() {
this.setupFCConfig = function(){
$('.nestedFunction').click(function(){
alert('setupFCConfig func()');
});
};
$(".edit").on('click', function(){
alert('edit click func()');
});
$(".cancel").on('click', function(){
alert('cancel click func()');
});
$(".checkout").click(function(){
alert('checkout click func()');
});
return this;
};
})( jQuery );
var page = $.fn.investPage();
page.setupFCConfig();
JSFiddle
The second returns the investPage
object where you can than access the function from the object variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With