为什么值被添加,但数组不被推送?增加了可变值,但数组未被推送
我想,如果我在功能后加window.load
它工作。我知道,当它最初被解析时,函数不会运行,但是这种情况下,我在窗口加载后立即调用函数。
无论如何,变量为什么会得到值而不是数组?我真的在用愚蠢的方式使用window.load
吗?在这里学习。
window.load = foo(); //works if I declare this after the function
var barr = [],
max, min;
function foo() {
var a = 10,
b = 30;
barr.push("asfd");
min = Math.min(a, b);
max = Math.max(a, b);
console.log(barr); // IDK what does this log, DevTool just throws error straight away
};
console.log(barr); // logs empty array [] as initially declared
console.log(min); // logs 10 on global scope
console.log(max); // logs 30 on global scope
此外事件处理程序是window.onload不是window.load –
谢谢,但你尝试运行你的代码?我做了,数组仍然没有推动。我曾经得到最小值和最大值,但barr未定义为'window.onload',但无法再次重现。 我明白了关于最小和最大的观点,但是阵列应该被推向正确? –
@СхирисхШреста请读回答的最后一句 – Tushar