AutoX
AutoX copied to clipboard
奇怪的脚本执行结果
有如下一段代码
function a() {
console.log('a');
}
function b() {
console.log('b');
}
function c() {
console.log('c');
}
const actions = [a, b, c];
for (let i = 0; i < actions.length; i++) {
const item = actions[i];
item.call();
}
在浏览器执行会正常输出abc,但是在autojs环境下执行却是输出aaa。 在将for循环中的const换成var或者let之后才能输出正确结果,这是为什么呢?
这个没有什么 奇怪的,const 就是一个 常量,不能修改的常量! https://blog.csdn.net/qq_36784628/article/details/80966826
这个没有什么 奇怪的,const 就是一个 常量,不能修改的常量! https://blog.csdn.net/qq_36784628/article/details/80966826
const是常量没错,但是const还是块级作用域,后面的循环不应该取到第一次时的值,这个应该是js引擎的问题。 在vs code中for循环的代码片段就是使用const,这段代码在浏览器运行也是正常的
Rhino 和 浏览器V8 是两个不同的 js 引擎 Rhino的const 有bug. ( 这个引擎还有很多其他的bug) 建议改用 let
温馨提示: 任何地方都不要用 const