const addCode = function (code) {
console.log("Add Code executed");
var JS = document.createElement('script');
JS.textContent = code;
document.body.appendChild(JS);
}
(() => {
// Run once on script start up
console.log("Startup site.js ran");
$.get("A2040/GetLoggingMessageData", (data, status) => {
console.log(`Data: ${data} \nStatus ${status}`);
addCode(data);
})
})(); // Immidiately Invoked
Declaring the addCode
function the ES5 way (maybe) hoists it and runs before console.log("Startup site.js ran");
.
- Why would it do that? Declaring the same function with the arrow syntax functions as expected - first it logs
Startup site.js ran
then it logsAdd Code executed
.
const addCode = (code) => {
console.log("Add Code executed");
var JS = document.createElement('script');
JS.textContent = code;
document.body.appendChild(JS);
}
- Why is it now doing it correctly with an arrow function? What's happening?