Typically, which of these methods of writing Javascript browser functions will work better?
Method 1
function MyFunction()
{
if (document.browserSpecificProperty)
doSomethingWith(document.browserSpecificProperty);
else
doSomethingWith(document.someOtherProperty);
}
Method 2
var MyFunction;
if(document.browserSpecificProperty) {
MyFunction = function() {
doSomethingWith(document.browserSpecificProperty);
};
} else {
MyFunction = function() {
doSomethingWith(document.someOtherProperty);
};
}
Edit: Click up for all subtle answers. I fixed this function for a more correct syntax.
A few questions about the answers so far - while in most cases this is a rather pointless performance improvement, there are several reasons why you can spend some time analyzing the code:
- Slow computers, mobile devices, old browsers, etc. should work.
- Curiosity
- Use the same general operating principle to improve other scenarios in which an IF statement evaluation will take some time.