简体   繁体   English

回调函数是否在javascript中创建新范围

[英]Does the callback function create a new scope in javascript

function hello() {
  var result = [];
  var str = 'I am here';
   function inner() {
    var result = [];
     for (var i=0;i<10;i++) {
        result.push(i);
     }
     return result;
   }
}

in the code above when I called the function hello(), the return value was an empty [], also it did not console.log the str from the inner function. 在上面的代码中,当我调用函数hello()时,返回值为空[],也没有console.log来自内部函数的str。 However, in the below code set I have used a call back function: 但是,在下面的代码集中我使用了一个回调函数:

 function forEach(cb) {
   for(var i =0;i<10;i++) {
    cb(i);
   }
}

function hello() {
   var result = [];
   var str = 'I am here';
   forEach(function(num) {
    console.log(str);
     result.push(num);
   }); 
   return result;
}

the question is why both functions reacted, and gave a different output? 问题是为什么两个函数都反应了,并给出了不同的输出? Note; 注意; in both codes there was an inner function which supposed to create a new scope that can access the outer scope ? 在两个代码中都有一个内部函数,它应该创建一个可以访问外部作用域的新作用域? Does anyone have a good explanation for this issue ? 有没有人对这个问题有一个很好的解释? Thanks 谢谢

In the first code block, inner is a new function that is declared inside of hello . 在第一个代码块中, inner是一个在hello声明的新函数。 It does not execute because you do not call it. 它不会执行,因为你没有调用它。 It does create a new child scope inside of hello when it is called. 它在调用时会在hello中创建一个新的子范围。 But, since hello() doesn't actually return anything and doesn't call inner() , you get undefined when you call hello() . 但是,由于hello()实际上并没有返回任何内容并且不调用inner() ,因此在调用hello()时会得到undefined You could fix that by changing to this (you can run this snippet to see the return value): 您可以通过更改为此来修复此问题(您可以运行此代码段以查看返回值):

 function hello() { var str = 'I am here'; // declare inner function inner() { var result = []; for (var i = 0; i < 10; i++) { result.push(i); } return result; } // now call inner() and return its result return inner(); } console.log(hello()); 

Functions declared inside other functions do create a new scope. 在其他函数内声明的函数会创建一个新范围。 Every function declaration and corresponding call in Javascript creates a new scope. Javascript中的每个函数声明和相应的调用都会创建一个新的范围。 If the function is inside another function, then the code inside that inner function has access to both its local scope and to the parent scope and this can nest as deep as your nested function declarations go. 如果函数在另一个函数内,那么该内部函数内的代码可以访问其本地作用域和父作用域,这可以嵌套到嵌套函数声明的深度。

the question is why both functions reacted, and gave a different output? 问题是为什么两个函数都反应了,并给出了不同的输出? Note; 注意; in both codes there was an inner function which supposed to create a new scope that can access the outer scope ? 在两个代码中都有一个内部函数,它应该创建一个可以访问外部作用域的新作用域? Does anyone have a good explanation for this issue ? 有没有人对这个问题有一个很好的解释? Thanks 谢谢

In the first example, you never called the inner function so it never executed. 在第一个示例中,您从未调用内部函数,因此它从未执行过。


In the second example, you pass an inline, anonymous function reference as a function argument to your forEach() function. 在第二个示例中,将内联的匿名函数引用作为函数参数传递给forEach()函数。 When that forEach() function executes, it calls your callback function with the line of code cb(i) so that's how your callback function is getting called in the second example. 当执行forEach()函数时,它会使用代码行cb(i)调用回调函数,这样就可以在第二个示例中调用回调函数。 forEach() calls it for you. forEach()为你调用它。

Also, in the second example, the locally declared callback function is accessing the parent scope and modifying the result array (which is perfectly allowable). 此外,在第二个示例中,本地声明的回调函数正在访问父作用域并修改result数组(这是完全允许的)。 In your first code example, you are declared a new result variable in the inner function scope and then returning that. 在您的第一个代码示例中,您在inner函数作用域中声明了一个新的result变量,然后返回它。 You are not accessing the parent result variable. 您没有访问父result变量。 When you declare a local variable with the same name as a variable in the parent scope, the local variable overrides the parent variable (essentially hiding the parent variable) and any references to that variable in the local scope only access the local variable. 当声明一个与父作用域中的变量同名的局部变量时,局部变量会覆盖父变量(基本上隐藏父变量),并且对本地作用域中该变量的任何引用只访问局部变量。

You could have written the first code example to use a parent scoped result variable like this: 您可以编写第一个代码示例来使用父作用域result变量,如下所示:

 function hello() { var result = []; var str = 'I am here'; // declare inner function inner() { for (var i = 0; i < 10; i++) { result.push(i); } return result; } // now call inner() and return result inner(); return result; } console.log(hello()); 

You have confused function declaration and calling. 你混淆了函数声明和调用。

Declaring a function can be done in multiple ways: 声明函数可以通过多种方式完成:

function myNewFunction() {}

Now this function exists in the current scope, but will not execute unless called. 现在,此函数存在于当前作用域中,但除非被调用,否则不会执行。

You can call the function like this. 你可以像这样调用这个函数。 Now your function will get executed. 现在你的功能将被执行。

myNewFunction();

Functions and their scopes can be compared to variables. 可以将函数及其范围与变量进行比较。 Functions defined inside another function can only be accessed from inside the parent function. 在另一个函数内定义的函数只能在父函数内部访问。 Let me give you an example. 让我给你举个例子。

  function myMegaFunction() { console.log("in mega"); function mySmallFunction() { console.log("in small"); } } myMegaFunction(); 

This will only print - 'in mega'. 这只会打印 - '在mega'。 If you call mySmallFunction() inside of myMegaFunction then both lines will get printed. 如果调用mySmallFunction()myMegaFunction那么这两条线将得到打印。


Now let's take a more complex scenario like this: 现在让我们采取更复杂的方案,如下所示:

  function myMegaFunction() { console.log("in mega"); var str = "car"; var result = function mySmallFunction() { console.log("in a small", str); } return result; } var resultingFunction = myMegaFunction(); resultingFunction(); 

This will first print the following: in mega in a small car 这将首先打印以下内容: in mega in a small car

So essentially, this is functions being passed around like variables. 基本上,这是像变量一样传递的函数。 They can be executed later on too. 它们也可以在以后执行。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM