English 中文(简体)
在每一次在环形体内创造新空间的语文中,在新范围内,每次都制作新的当地环形变量副本?
原标题:In languages which create a new scope each time in a loop block, a new local copy of the local loop variable is created each time in that new scope?

似乎在C、Java和Ruby(而不是Javascript)等语种中,为整整整整整整整整整块创造了新的空间,而为 lo而界定的当地变量实际上每一次都变成一个地方变量,并记录在这一新范围内?

例如,在鲁比:

p RUBY_VERSION

$foo = []

(1..5).each do |i|
  $foo[i] = lambda { p i }
end

(1..5).each do |j|
  $foo[j].call()
end

印刷版是:

[MacBook01:~] $ ruby scope.rb
"1.8.6"
1
2
3
4
5
[MacBook01:~] $ 

因此,它像在设定新范围时一样,还在这一新范围内制作和记录新的<代码>i的当地文本,以便在以后履行这一职能时,“i”分别存在于这些范围链中,即1、2、3、4、5。 情况如何? (它像一项大规模行动一样健全)。

违反

p RUBY_VERSION

$foo = []

i = 0

(1..5).each do |i|
  $foo[i] = lambda { p i }
end

(1..5).each do |j|
  $foo[j].call()
end

此时此刻,<代码>i在进入左轮机之前就作了界定,因此,Ruby 1.1.8.6将不会将i列入为左轮机组规定的新范围,因此,如果在范围链上对<代码>i进行探讨,则总是指在外面的i,并每次给予5:

[MacBook01:~] $ ruby scope2.rb
"1.8.6"
5
5
5
5
5
[MacBook01:~] $ 

我听说,在Rub.1.9,i,即使在前面界定了i时,仍将作为当地界定的休息地点对待?

建立新范围的运作,产生了一个新的当地版i 每次穿透似乎都很重,因为如果我们不在今后援引这些职能,那似乎就没有问题了。 因此,当职能不需要在稍后时间援引时,口译人员和C /Juan编审员可尝试优化职能,以便没有当地文本的<代码>i<。 每次?

最佳回答
问题回答

暂无回答




相关问题
Closure/scope JavaScript/jQuery

I m trying to group some exisiting top-level functions inside a closure (to avoid polluting the global namespace) but I m not quite getting it to work. First, all the JS works outside my anonymous ...

Nested Lambdas in Python

I m a beginning python programmer, and I d like someone to clarify the following behavior. I have the following code: env = lambda id: -1 def add(id, val, myenv): return lambda x: val if x == ...

closures mean fully type-safe criteria?

combining closures (FCM) and generics, would it be possible to have fully type-safe criteria. // The following works without a cast as Foo.id is a long field. List<Long> ids = session....

Is nested XMLHttpRequests with multiple closures a good idea?

I have a Greasemonkey script which operates on a search results page at a video site. The function of the script is to take a javascript link that opens a new window with a flash player, jump through ...

How|Where are closed-over variables stored?

This is a question based on the article "Closing over the loop variable considered harmful" by Eric Lippert. It is a good read, Eric explains why after this piece of code all funcs will return the ...

Scope with a self-invoking function in Javascript

Take below code iterates over 6 input buttons and attaches an onclick event to every button that alerts the index number of the respective iteration: for (var i = 1; i < 6; ++i) { var but = ...

热门标签