What's the best way to conditionally define functions for popular modern browsers?
I'm seeing Safari 12 (iOS 12.0.1 and macOS 10.14) incorrectly define conditional functions while Chrome, Firefox, Edge seem to do the right thing. Additionally, what's the spec correct behavior for a JS engine so I could report this bug to Apple if it's a bug.
Background: We're doing this to avoid Google Analytics (gtag.js) from ever loading if a user doesn't consent to GDPR. Google's gtags.js boilerplate init code has a line function gtag(){dataLayer.push(arguments);}
which trips Safari.
Code below, demo at JS Fiddle here
<html>
<head>
<title>Safari JS engine test</title>
<script type="text/javascript">
if (false) {
function conditionalFunctionality() {
alert("Doing conditional functionality");
}
}
window.onload = function() {
if (typeof conditionalFunctionality === "function") {
alert("BAD. It's defined");
} else {
alert("GOOD. It's NOT defined");
}
}
</script>
</head>
<body>
<h1>Safari JS engine test</h1>
</body>
</html>