Well, too many mistakes to think about. First of all, I don’t understand if you want static references or instantiated values. You are not using jsDoc tags or anything like that. The compiler works best only with the corresponding jsDoc tag. You logic is very strange and poorly formulated. Alternation prototypes, etc., Everything happens in IIFE (immediately called function expression). Do your functions collide? Are they constructors? Are we human or are we a dancer?
IIFE is executed before the DOMContentLoaded event is fired by the browser. The most you can do is jQuery IIFE equivalent $(function() {})(); which associates this with a DOMReady or DOMContentLoaded callback. You define built-in functions inside blocks, which is not even in ECMA.
Although most script engines support function declarations within blocks, they are not part of ECMAScript (see ECMA-262 , clauses 13 and 14). Worse implementations are incompatible with each other and with future EcmaScript proposals. ECMAScript only allows function declarations in the list of root script or function statements. Instead, use a variable initialized with a function expression to define a function within a block.
var myFunctionName = function (params) {};
You also do not have enough half-column loads. Automatically inserting a semicolon insert into your JS interpretation is not entirely flawless, so get out of it.
Reliance on implicit insertion can cause subtle, hard-to-debug problems. Do not do this. You are better than that.
There are a couple of places where missing semicolons are especially dangerous:
// 1. MyClass.prototype.myMethod = function() { return 42; } // No semicolon here. (function() { // Some initialization code wrapped in a function to create a scope for locals. })(); var x = { 'i': 1, 'j': 2 } // No semicolon here. // 2. Trying to do one thing on Internet Explorer and another on Firefox. // I know you'd never write code like this, but throw me a bone. [normalVersion, ffVersion][isFF](); var THINGS_TO_EAT = [apples, oysters, sprayOnCheese] // No semicolon here. // 3. conditional execution a la bash -1 == resultOfOperation() || die();
So what is going on?
JavaScript error - the return 42 function is returned first with the second function as a parameter, then the number 42 is “called”, which leads to an error. Most likely, you will get “no such property in error undefined” at runtime because it is trying to call x[ffVersion][isIE]() . die is called if resultOfOperation() NaN and THINGS_TO_EAT get the result of die() . Why?
JavaScript requires instructions to end with a semicolon, unless it thinks it can safely infer their existence. In each of these examples, a function declaration or a literal of an object or array is used inside the statement. Closing brackets are not enough to signal completion of an instruction. Javascript never ends a statement if the next token is an infix or bracket operator.
It really surprised people, so make sure your assignments end with a semicolon.