Javascript 中 Tcl 的 'uplevel' 是什么?
What is the equivalent of Tcl's 'uplevel' in Javascript?
Tcl 的 uplevel
命令将 eval
提供的字符串放在比当前子例程更高的堆栈帧中。
在Javascript中有对应的吗?
在Tcl中最常见的使用uplevel
的情况是在JS中通过让调用者传入一个匿名函数来完成的。 (有时需要对变量范围进行欺骗。)
proc iterSquares {var script} {
upvar 1 $var v
for {set i 0} {$i <= 10} {incr i} {
set v [expr {$i ** 2}]
uplevel 1 $script
}
}
iterSquares x {
puts "I've got a $x"
}
function iterSquares(callback) {
for (var i = 0; i <= 10; i++) {
callback(i ** 2);
}
}
iterSquares(function(x) {
console.log("I've got a", x);
});
但是没有真正等同于更通用的形式,它们在很多方面更像 Lisp 宏扩展(尽管两者也不相同)。
Tcl 的 uplevel
命令将 eval
提供的字符串放在比当前子例程更高的堆栈帧中。
在Javascript中有对应的吗?
在Tcl中最常见的使用uplevel
的情况是在JS中通过让调用者传入一个匿名函数来完成的。 (有时需要对变量范围进行欺骗。)
proc iterSquares {var script} {
upvar 1 $var v
for {set i 0} {$i <= 10} {incr i} {
set v [expr {$i ** 2}]
uplevel 1 $script
}
}
iterSquares x {
puts "I've got a $x"
}
function iterSquares(callback) {
for (var i = 0; i <= 10; i++) {
callback(i ** 2);
}
}
iterSquares(function(x) {
console.log("I've got a", x);
});
但是没有真正等同于更通用的形式,它们在很多方面更像 Lisp 宏扩展(尽管两者也不相同)。