为什么 javascript 坚持在外部函数中发生的局部变量重新分配,而不必捕获 return 值?
Why does javascript persist local variable reassignments that occur in outside functions without having to capture the return values?
我是一名 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 中,我认为与此等价的东西会 return x equals 2
。
在研究了这段代码(获取用户 gps 坐标)后,我发现了 javascript(相对于 ruby)的这种独特品质
var currPosition;
navigator.geolocation.getCurrentPosition(function(position) {
updatePosition(position);
$("#lat").html(position.coords.latitude;);
$("#lng").html(position.coords.longitude);
};
function updatePosition(position) {
currPosition = position;
}
为什么在 getCurrentPosition
函数内部,position
变量更新为 updatePosition()
的 return 值,即使 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
而
var b = { count: 0 };
function increment(x) {
x = { count: x.count + 1 }; // changing variable's reference
}
increment(b);
console.log(b.count);
// => 0
等同于
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
在比函数更宽的范围内,有点但不太像在 [=54= 中使用 $currPosition
].这允许函数将值分配给一个可见的变量:
var c = 0; // outer scope
function update() {
c = 1;
}
update();
console.log(c);
// 1
但是
function update() {
var d = 0; // inner scope
d = 1;
}
update();
console.log(d);
// undefined
在Ruby中,变量不允许像这样跳转函数(方法)作用域,但你可以使用@a
、@@a
或$a
来访问一个外部范围(实例,class 或全局):
def update
c = 1 # local scope
end
update
puts c
# => Error
但是
@d = nil # instance scope
def update
@d = 1
end
update
puts @d
# => 1
但是,Ruby 中的块与 JavaScript 中的函数具有相似的范围效应:
e = nil # outer scope
1.times do
e = 1
end
e
# => 1
但是
1.times do
f = 1 # inner scope
end
f
# => Error
我是一名 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 中,我认为与此等价的东西会 return x equals 2
。
在研究了这段代码(获取用户 gps 坐标)后,我发现了 javascript(相对于 ruby)的这种独特品质
var currPosition;
navigator.geolocation.getCurrentPosition(function(position) {
updatePosition(position);
$("#lat").html(position.coords.latitude;);
$("#lng").html(position.coords.longitude);
};
function updatePosition(position) {
currPosition = position;
}
为什么在 getCurrentPosition
函数内部,position
变量更新为 updatePosition()
的 return 值,即使 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
而
var b = { count: 0 };
function increment(x) {
x = { count: x.count + 1 }; // changing variable's reference
}
increment(b);
console.log(b.count);
// => 0
等同于
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
在比函数更宽的范围内,有点但不太像在 [=54= 中使用 $currPosition
].这允许函数将值分配给一个可见的变量:
var c = 0; // outer scope
function update() {
c = 1;
}
update();
console.log(c);
// 1
但是
function update() {
var d = 0; // inner scope
d = 1;
}
update();
console.log(d);
// undefined
在Ruby中,变量不允许像这样跳转函数(方法)作用域,但你可以使用@a
、@@a
或$a
来访问一个外部范围(实例,class 或全局):
def update
c = 1 # local scope
end
update
puts c
# => Error
但是
@d = nil # instance scope
def update
@d = 1
end
update
puts @d
# => 1
但是,Ruby 中的块与 JavaScript 中的函数具有相似的范围效应:
e = nil # outer scope
1.times do
e = 1
end
e
# => 1
但是
1.times do
f = 1 # inner scope
end
f
# => Error