Safari const value test

by cx20

JavaScript

let code = `const SCALE = 1 / 10;   // TODO: "Can't find variable: SCALE" error in Safari

const createScene = function() {
    console.log("createScene()");
    addGround();
};

function addGround() {
    console.log("addGround()");
    console.log("width:" + 60 * SCALE);
}

window.initFunction = async function() {
    window.scene = createScene();
}
`;

function FastEval(code) {
  const head = document.getElementsByTagName("head")[0];
  const script = document.createElement("script");
  script.setAttribute("type", "text/javascript");

  script.innerHTML = `try {${code};}
          catch(e) {
              console.log(e);
          }`; // Error in Safari
  //script.innerHTML = `{${code};}`; // Error in Safari
  //script.innerHTML = `${code};`;  // No Errors in Safari

  head.appendChild(script);
}

async function init()  {
  FastEval(code);

	window.initFunction();
}

init();