简体   繁体   English

函数可以访问在其自己的父函数中定义的变量吗?

[英]Can functions access variables defined in its own parent function?

Mozilla's Developer Resource shows two contradicting explanation regarding the function scope. Mozilla的开发人员资源在功能范围上显示了两个矛盾的解释。

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

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()的范围无法访问subFunction1()的变量。 myBigFunction() also doesn't wrap subFunction1() which would provide a closure. myBigFunction()也没有包装subFunction1()来提供闭包。 Check out closures here . 在此处查看封包

However, add() is declared within the scope of the getScore() function. 但是, add()getScore()函数的范围内声明。 getScore() 'wraps' add() - providing a closure so that add() has access to variables within getScore() 's scope. getScore() '包装' add() -提供一个闭包,以便add()可以访问getScore()范围内的变量。

In 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. 这里myValue包含在函数myBigFunction()因此具有块范围。

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. 在上面的示例中,num1和num2在函数getScore()中声明,并且具有内部函数add()因为使用num1和num2的添加函数会将其作为闭包传递给add()函数,并且在访问时不会出现任何错误这些变量。

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. 要全面了解它,请通过JavaScript范围和闭包概念。

Use the this keyword to get parent variable ie 使用this关键字获取父变量,即

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!) (希望这可以帮助!)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 可以回调函数访问父函数变量 - can callback functions access parent function variables 量角器:无法访问父函数中定义的闭包中的变量 - Protractor: Can not access variables in closure defined in parent function 为什么我的内部函数无法访问其父函数中定义的对象变量? - Why doesn't my inner function have access to an object variable defined in its parent function? 在函数中定义为同级的函数是否可以访问其同级局部变量? - Will a function defined within a function as a sibling have access to its sibling local variables? 动态访问定义的函数或变量属性 - Dynamically access defined functions or properties of variables 如何在可以访问其变量的javascript中向对象添加函数 - how to add function to an object in javascript that can access its variables 函数可以从先前的调用中访问其变量吗? - Can a function access its variables from previous invocation? 解释为什么Javascript中的匿名函数可以访问外部函数中的变量? - Explain why anonymous functions in Javascript can access variables in the outer function? 为什么在定义函数时闭包允许函数访问范围内的外部变量有什么关系? - Why does it matter that closures allow functions to access external variables that are in scope when the function is defined? 嵌套函数可以使用在父函数中其他函数中设置了值的变量吗? (JavaScript) - Can nested functions use variables with values set in other functions within the parent function? (Javascript)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM