window.onresize = window.onload = function(){
if(window.innerWidth < 480){
document.getElementById('alert').onclick = function(){
alert('<480');
};
//large amounts of code
}
if(window.innerWidth >= 480){
document.getElementById('alert').onclick = function(){
alert('>=480');
};
//large amounts of code
}
}
<button id="alert">Alert</button>
In the above code if window.innerWidth is greater than 480, will the code inside first if be processed by the Javascript engine? The second block will be executed and I will have the function in memory and assigned to #alert.onclick.
The question is will the function inside the other(false) condition be there in memory as a variable may be like a dangling reference or will that function be bootstrapped only when the condition is true?
This is to understand if there is any advantage in terms of initial amount of code processed when window loads if the code for mobile is inside conditional statements like this and is considerably large..
I'll be glad to see any documentation on how a function is bootstrapped and stacks allocated and when.
document.getElementById('alert').onclick = function () { ... }) are not hoisted. See: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…