For example
var w = document.getElementById;
var b = w('header');
var c = w('footer');
var d = w('body');
Edit: Semicolons are another one of those big arguments. I thought I would edit the question for fun.
Edit: Responses to Andrey's comments found on his answer.
"How does copying reference make it more effective with JS compilers?"
Response: JS compilers are to shorten and/or obfuscate code. If there were 40 calls to document.getElementById(..)
, it would be much more compact if they called getById(..)
which would be renamed to something like O(..)
.
"Also, when you handle html element events, you usually specify a js method, and inside the method you put the logic, not directly in the html event handlers - that is not required but a good practice"
Response: I know. But we have many many web systems and they rarely follow good practice completely.
"Also, using built in methods directly makes the code way more readable" Response: Given these two examples, I think the latter is more readable
document.getElementById('total').value = document.getElementById('subtotal').value + document.getElementById('salestax').value - document.getElementById('discount').value
document.getElementById('yousaved').value = document.getElementById('discount').value / (document.getElementById('subtotal').value + document.getElementById('salestax').value)
or
var byId = document.getElementById
byId('total').value = byId('subtotal').value + byId('salestax').value - byId('discount').value
byId('yousaved').value = byId('discount').value / (byId('subtotal').value + byId('salestax').value)