0

Function Scopes

How do I call a function inside a function from another function? code: function a() { //here, i would like to call c() //inside function b(). } function b() { function c() { alert("yay!"); } } a(); //call function a

16th Oct 2017, 3:15 PM
Kyle Christian Orilla
Kyle Christian Orilla - avatar
4 Respostas
+ 5
@Carl Fies: You're wrong... when functions are declared using keyword 'function' rather that 'var' (ie: 'var f = function() {}' instead 'function f() {}'), they are parsed first and are available at run time even they are not already reached by interpreter (it had be done in a previous pass) ^^ You'll be right if functions were declared as variable assignement ;P @Kyle: Functions declared inside another one are only accessible from function scope, or descendant scopes, never from outside, as for any variables... In fact, doing: function f() {} is same as var f = function() {} (but reference is not assigned at same time)... so accessing a function through different scopes follow sames rules as variable access... Anyway, you can workaround if you get the function reference from inside and export it in an outside scope (in your example case, that doesn't make lot sense, but if you have more than once function declared in b(): var f={}; //object outside functions scopes to share references b(); //call function b to initialize f a(); //call function a to use function c and/or d inside function b, but through reference stored in f function a(){f.c();} function b(){ f.c=c; f.d=d; function c(){alert("yay!");} function d(){alert("hop!");} } Or use an object litteral or object constructor, or even just add "public" property on a previous declared function, as they are specific but regular JS objects... last case in example: function b(){} b.c=function() {alert("yay!");}; ... In this case, function b doesn't need to be called before using b.c, but b.c need to be initialized when you call it, as said by @Carl ;) @Schindlabua is right, but there's some subtilities: function c declared inside function b create one each time b is called, AND create a new local variable scope context each time: this could be useful to handle "private" values: function b(arg) {return function c(){return arg; }} var a=b(5); a(); //return 5 var d=b(42); d(); //return 42 a(); //still return 5
17th Oct 2017, 6:13 PM
visph
visph - avatar
0
you just call the function. please be aware that calling and implementing a function are two pairs of shoes. You always need to implement the function before you can call it. in your case start with function c(). then b() then a()
16th Oct 2017, 3:49 PM
Carl Fies
Carl Fies - avatar
0
c is not defined. lol here's the code. https://code.sololearn.com/WjuvHjVqlOXZ/?ref=app
16th Oct 2017, 4:24 PM
Kyle Christian Orilla
Kyle Christian Orilla - avatar
0
You can't call c from within a. Everytime you call b, a new function c will be created, so it's not even clear which c you are talking about!
16th Oct 2017, 4:26 PM
Schindlabua
Schindlabua - avatar