回调/作用域理解 [英] Callback/scope understanding

查看:31
本文介绍了回调/作用域理解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在使用回调的概念,并且遇到了一种情况,我想确保我知道自己在想什么,实际上正在发生.

So i was playing around with the concept of callbacks and i ran into a situation where I wanted to make sure i knew what i thought was happening, was actually happening.

function greet(callback) { // 'greet' function takes a callback
  var greeting = "hi";     // 'greeting' variable is set in the scope
  callback()               // 'callback' function is invoked 
}

greet(
function() {
  console.log(greeting)
}); 

//Console logs nothing

很奇怪,您可能希望callback()查找其下一个最接近的范围并弄清楚问候是什么.但是,一旦我向全局执行上下文声明了greeting变量,问候语便会记录在控制台中.

Sorta weird, you'd expect the callback() to look in its next closest scope and figure out what greeting is. However, once i declare the greeting variable to the global execution context, greeting is logged in the console.

var greeting = 'hi'        // 'greeting' variable is set in global context

function greet(callback) { // 'greet' function takes a callback    
  callback()               // 'callback' function is invoked 
}

greet(
function() {
  console.log(greeting)
}); 

//Console logs: 'hi'

这是因为从技术上来说,记录变量的callback()函数实际上是在全局上下文中定义的,并且只是在greet()内部调用?因此,它不会像普通的函数表达式那样首先在greet()内部查找,而是直接进入全局上下文,因为它是在此定义的.

Is this because technically the callback() function that's logging the variable is actually defined in the global context and is simply being invoked inside of greet()? So it's not going to look inside of greet() first, like a normal function expression would, but right into the global context because that's where it's defined.

我只是想确保我了解这里发生的事情,而不是我没有意识到的一些奇怪的范围/块问题.

I just wanted to make sure i understand what's happening here and not some weird scope/block issue that i don't realize.

推荐答案

您是对的-该函数可以访问定义它的作用域.

You're right - the function has access to the scope which it is defined in.

您的示例实际上是这样的(将回调函数移至其自己的函数):

Your example is effectively this (moving your callback to its own function):

var greeting = 'hi';

function greet(callback) {
    callback();
}

function logGreeting() {
    console.log(greeting);
}

greet(logGreeting);

如您所见, logGreeting 现在可以访问 greeting ,因为它是在相同(或更高)范围内定义的.

As you can see, logGreeting has access to greeting at this point because it is defined in the same (or higher) scope.

但是,当我们将 greeting 移到 greet 函数中时:

However, when we move greeting into the greet function:

function greet(callback) {
    var greeting = 'hi';
    callback();
}

function logGreeting() {
    console.log(greeting);
}

greet(logGreeting);

greeting 不再与 logGreeting 处于相同或更高的范围内,它完全处于不同的范围内,因此 logGreeting 不能向上看范围以找到 greeting .

greeting is no longer in the same or higher scope to logGreeting, it's in a different scope entirely, so logGreeting cannot look upwards through the scope in order to find greeting.

logGreeting 可以访问的范围是定义它的范围,而不是被调用的范围,因此,当使用 callback()调用它时,它不会评估该范围.就像您在问题中提到的那样.

The scope which logGreeting has access to is the scope in which it is defined rather than which it is called, so it doesn't evaluate the scope when it's called with callback() like you mentioned in your question.

这篇关于回调/作用域理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆