I have such a js code:
function Global(params) {
...
}
var g = {
onDocLoad: function() {
// on DOM load I instantiate the Global function-constructor
// which is above this line, so it is already in scope
var global = new window.Global(params);
}
window.addEventListener("load", g.onDocLoad, false);
If I use this file as a static, without precompilation, everything works fine.
If I precompile it:
rake assets:precompile:all RAILS_ENV=development
and then refresh the page I get:
Uncaught TypeError: object is not a function
right exactly on the line with new window.Global(params);
. It says that Global
is not a function. But it cannot be true. It is a function.
I debugged that line and found that at the time when this line is reached the Global
is null
in global scope. There is no such a function with such a name.
My issue is similar to these questions:
Javascript "Uncaught TypeError: object is not a function" associativity question
After running rake assets precompile, getting undefined in not a function
but their solutions are not applicable in my case.
What may be the problem? Why precompitation breaks down working code?