Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/420.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么javascript会保留在外部函数中发生的局部变量重新分配,而不必捕获返回值?_Javascript_Ruby_Scope_Variable Assignment - Fatal编程技术网

为什么javascript会保留在外部函数中发生的局部变量重新分配,而不必捕获返回值?

为什么javascript会保留在外部函数中发生的局部变量重新分配,而不必捕获返回值?,javascript,ruby,scope,variable-assignment,Javascript,Ruby,Scope,Variable Assignment,我是一名ruby程序员,所以我通常来自哪里,如果你有这样的经历: def addone(x) x+1 end def ruby_assignment_behavior(x) addone(x) puts "x equals " + x end 运行最后一个方法将导致以下结果: ruby_assignment_behavior(1) #=> "x equals 1" 在javascript中,我认为与此等价的东西将返回x=2 在研究了这段代码(获

我是一名ruby程序员,所以我通常来自哪里,如果你有这样的经历:

  def addone(x)
    x+1
  end

  def ruby_assignment_behavior(x)
    addone(x)
    puts "x equals " + x
  end
运行最后一个方法将导致以下结果:

ruby_assignment_behavior(1)
#=> "x equals 1"
在javascript中,我认为与此等价的东西将返回
x=2

在研究了这段代码(获取用户gps坐标)后,我发现了javascript的独特特性(相对于ruby)

为什么在
getCurrentPosition
函数中,
position
变量用
updatePosition()
的返回值更新,即使
position
是闭包(?)中的局部变量

此外:
我很好奇,在javascript代码示例中,是否有必要在
getCurrentPosition
之外使用
updatePosition
函数,如果有,为什么会这样?在最外层范围内定义的
currPosition
变量是否携带重新分配的
position
值?

这两段代码非常不同。在Ruby代码中,您正在更改变量值。由于变量是局部变量,正如您正确声明的那样,因此更改不会反映在范围之外

在JavaScript代码中,您正在更改变量指向的对象的内部状态。变量本身不会改变

在这方面,Ruby和JavaScript的行为是相同的

var a = { count: 0 };
function increment(x) {
  x.count++; // variable changed, changing referenced object state
}
increment(a);
console.log(a.count);
// => 1
相当于

a = { count: 0 }
def increment(x)
  x[:count] += 1 # variable changed, changing referenced object state
end
increment(a)
puts a[:count]
# => 1
b = { count: 0 }
def increment(x)
  x = { count: x[:count] + 1 } # changing variable's reference
end
increment(b)
puts b[:count]
# => 0

相当于

a = { count: 0 }
def increment(x)
  x[:count] += 1 # variable changed, changing referenced object state
end
increment(a)
puts a[:count]
# => 1
b = { count: 0 }
def increment(x)
  x = { count: x[:count] + 1 } # changing variable's reference
end
increment(b)
puts b[:count]
# => 0
函数外部的
var currPosition
声明变量
currPosition
在比函数更宽的范围内,这有点像在Ruby中使用
$currPosition
。这允许函数将该值分配给一个变量,该变量将显示在函数中:

var c = 0; // outer scope
function update() {
  c = 1;
}
update();
console.log(c);
// 1
但是

在Ruby中,不允许变量跳转这样的函数(方法)作用域,但可以使用
@a
@@a
$a
访问外部作用域(实例、类或全局):

但是

但是,Ruby中的块与JavaScript中的函数具有类似的作用域效果:

e = nil # outer scope
1.times do
  e = 1
end
e
# => 1
但是


在代码示例中,以
var a=0;//outerscope
,这两个代码示例是否被解释为在同一外部范围内按顺序执行?啊,不,是独立的代码段(如果你一个接一个地执行它们,就会产生污染)。我可能应该重命名变量以避免。。。编辑:完成。如果javascript
c
&
d
示例按顺序使用相同的变量名运行,那么第二个
c
操作不会将
c
转换为闭包内的局部变量,从而使其在闭包外返回null吗?我会测试一下。。。是的,当然有。没有蜕变。在ES5中,变量存在于声明它的作用域(使用
var
)及其内部的所有作用域;如果没有
var
,则变量为全局变量(范围最广)。因此,
c
存在于整个代码段中(这都是一个范围)
d
只存在于函数中。哦,如果它们都是
c
,并且您一个接一个地运行它们?然后重新定义的
update
将有一个局部变量
c
,它与外部范围变量
c
无关;重新定义的
更新
不会影响外部范围
c
def update
  c = 1 # local scope
end
update
puts c
# => Error
@d = nil # instance scope
def update
  @d = 1
end
update
puts @d
# => 1
e = nil # outer scope
1.times do
  e = 1
end
e
# => 1
1.times do
  f = 1 # inner scope
end
f
# => Error