admin管理员组文章数量:1391955
Mozilla's Developer Resource shows two contradicting explanation regarding the function scope.
Here indicates that even though a "variable is defined in the same scope as the function calls", the code will throw an error because "it is not defined inside the function definitions".
Example)
function myBigFunction() {
var myValue;
subFunction1();
}
function subFunction1() {
console.log(myValue);
} //this will show a reference error
However, here indicates that a "function defined inside another function can also access all variables defined in its parent function and any other variable to which the parent function has access".
Example)
function getScore() {
var num1 = 2,
num2 = 3;
function add() {
return name + ' scored ' + (num1 + num2);
}
return add();
} //this is ok
Mozilla's Developer Resource shows two contradicting explanation regarding the function scope.
Here indicates that even though a "variable is defined in the same scope as the function calls", the code will throw an error because "it is not defined inside the function definitions".
Example)
function myBigFunction() {
var myValue;
subFunction1();
}
function subFunction1() {
console.log(myValue);
} //this will show a reference error
However, here indicates that a "function defined inside another function can also access all variables defined in its parent function and any other variable to which the parent function has access".
Example)
function getScore() {
var num1 = 2,
num2 = 3;
function add() {
return name + ' scored ' + (num1 + num2);
}
return add();
} //this is ok
Share
Improve this question
asked Feb 19, 2019 at 2:25
KevvvKevvv
4,03312 gold badges52 silver badges105 bronze badges
5
- Both are not contradictory but actually plementary. variable scopes goes like this function<-parent-function<---parents<---global. In first example the variable is in local scope so its not visible to other function. But in second example its inside a function which bees its parent function. So it can access parent function's scope. – binariedMe Commented Feb 19, 2019 at 2:30
- The variables are declared in the parent function for both examples though – Kevvv Commented Feb 19, 2019 at 2:41
- 2 In first example there is no parent function. parent function are those which encapsulate a function. In first example, two separate function are defined. – binariedMe Commented Feb 19, 2019 at 2:42
-
1
subFunction1();
is just called within the parent function and defined outside of parent function. Defined function signature isfunction
subFunction1() {...
– zer00ne Commented Feb 19, 2019 at 2:50 - 2 So is it fair to say that where the function's definition is declared matters? – Kevvv Commented Feb 19, 2019 at 2:50
3 Answers
Reset to default 2In your first example
function myBigFunction() {
var myValue;
subFunction1();
}
function subFunction1() {
console.log(myValue);
} //this will show a reference error
here myValue is included in a function myBigFunction()
so it has a block scope.
Block scope means any code within that block (here function defines the block) can use that variable if function contain another function(function definition) and inner function using that variable then it will be passed in a closure to inner function. 2nd scenario exactly same what I explained here.
function getScore() {
var num1 = 2,
num2 = 3;
function add() {
return name + ' scored ' + (num1 + num2);
}
return add();
} //this is ok
In above example num1 and num2 are declared in function getScore()
, and it has inner function add()
since add function using num1 and num2 it will be passed as closure to add()
function and there won't be any error while accessing those variables.
but in first example the variable is accessed out of scope and as the variable accessed outside of that function it won't be accessible and it will throw reference error.
I hope the explanation clear your understanding. to understand it thoroughly go through JavaScript scope and closure concept.
The reason why the first example fails and the second 'works' has to do with scope and closure. You can think of a function's 'curly braces' as the enclosure around its scope - or sphere of influence.
The first example fails because myBigFunction()
's variable is not accessible to subFunction1()
's scope. myBigFunction()
also doesn't wrap subFunction1()
which would provide a closure. Check out closures here.
However, add()
is declared within the scope of the getScore()
function. getScore()
'wraps' add()
- providing a closure so that add()
has access to variables within getScore()
's scope.
Use the this
keyword to get parent variable i.e.
var bar = function () {
this.foo = "example";//the only catch is you have to have the this key word when defining the term.
var subfunction = function () {
this.foo = "This an example!";
}
}
(Hope this helps!)
本文标签: javascriptCan functions access variables defined in its own parent functionStack Overflow
版权声明:本文标题:javascript - Can functions access variables defined in its own parent function? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744696885a2620333.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论