为什么没有访问范围的变量? [英] Why is there no access to a variable frome a scope?

查看:78
本文介绍了为什么没有访问范围的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好

这是一个小问题:为什么Opera / Chrome无法访问外部LexicalEnvironment的[[scope]]中的值变量?

Hello
Here is a small question: why does Opera/Chrome have no access to the value variable from the [[scope]] of the external LexicalEnvironment?

function f() {
  var value = Math.random();

  function g() {
    debugger; // here we should take a look at the console and write
// alert( value );
  }

  return g;
}

var g = f();
g()

推荐答案

你的问题没有多大意义,你的示例代码没有显示你的内容正在做。



这是标准的东西。在声明的范围之外不能看到变量。如果在函数内声明变量,则其范围仅限于该函数和该函数内的任何成员。
Your question doesn't make a lot of sense and your example code doesn't show what you're doing.

This is standard stuff. A variable cannot be seen outside of the scope it is declared in. If a variable is declared inside a function, its scope is limited to that function and any members inside that function.


是的,奇妙的是,可以访问变量范围。这是一个非常有趣的现象。



你的功能非常有意义并且确实有效。为了显而易见,取消注释 alert

Yes, wonderfully, there is the access to the variable scope. This is a very interesting phenomenon.

Your function makes perfect sense and actually works. To make it apparent, it's enough to uncomment alert:
function f() {
  var value = Math.random();

  function g() {
    alert(value);
  }

  return g;
}

var g = f();
g()

我不知道为什么你没看到它;或者你想要实现不同的东西,然后解释它。



很难解释为什么这段代码有效。变量 value 是局部变量,在调用 f 后它不应该存在。这个技巧很有效,因为JavaScript实现了一个非常重要的效果, closure ,这是编程中的基本概念之一,尤其是功能概念。事件是在函数(也是 f context的本地对象)中使用的事实。这是一种双重封闭。首先,返回 g 的事实将 g 的范围扩展到外部范围。然后,在 g 中使用的事实,扩展值的范围也是。请阅读:

http://en.wikipedia.org/wiki/Closure_( computer_programming) [ ^ ]。



您可以实际修改代码,以便在每次调用时获取变量 value 的值,作为从功能。并不是说它在这个基本的例子中具有任何实际意义;它只是关闭效果的另一个例子。



在某种程度上,通过闭包的效果,本地对象仍然是本地的可见性,但在生命周期和间接可访问性方面表现得像外部范围对象。



-SA

I have no idea why you failed to see it; or perhaps you wanted to achieve something different, but then explain it.

It's harder to explain why this code works. The variable value is the local variable, it should not exist after the call to f. The trick works because JavaScript implements a very non-trivial effect, closure which is one of the fundamental notions in programming, especially functional concepts. The fact that the variable is used in a function (also the object local to f context) which is returned. This is a kind of "double closure". First, the fact that g is returned expands the scope of g to outer scope. And then, the fact that value is used in g, expands the scope of value, too. Please read about it:
http://en.wikipedia.org/wiki/Closure_(computer_programming)[^].

You can actually modify the code to obtain the value of the variable value on each call as a value returned from the function. Not that it makes any practical sense in this rudimentary example; it would be just another illustration of the closure effect.

In a way, through the effect of closure a local object remains local in terms of visibility, but behaves like an outer scope object in terms of life cycle and indirect accessibility.

—SA


这篇关于为什么没有访问范围的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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