为什么该代码块会引发错误“未捕获的ReferenceError:未定义setSize”
const canvas = document.querySelector("#draw");
(function setSize() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
})();
window.addEventListener("resize", () => {
setSize();
})
答案 0 :(得分:3)
只需定义您的setSize
函数并调用一次即可(它的作用与IFFE相同)。
const canvas = document.querySelector("#draw");
function setSize() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
setSize();
window.addEventListener("resize", () => {
setSize();
})