Kriss Morra
Kriss Morra

Reputation: 13

How to prevent/avoid of script execution during work of js interpreter

example of js file that will be parsed during loading:

var Test = {
   someProp: Utils.getProp()
}
var Utils = {
   myAttr: "",

   getProp: function() { 
      alert("Test"); 
   }
} 

The error that I'm getting during page loading is

"TypeError: Utils is undefined"

As far as I understand js interpreter is executing script during page load and in such case the error is occuring. The question here: is it real to load the page and to avoid the script execution? or how can I avoid the error?

Upvotes: 0

Views: 65

Answers (1)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324740

Since Test depends on Utils, Utils must be defined first.

var Utils = {
   myAttr: "",

   getProp: function() { 
      alert("Test"); 
   }
} 
var Test = {
   someProp: Utils.getProp()
}

Upvotes: 2

Related Questions