Loading ...
Sorry, an error occurred while loading the content.

560[jslint] Re: Undefined variable

Expand Messages
  • sandyhead25
    May 31, 2009
      I have encountered this problem before on my company website. When two identical object names, including functions, exist in the same namespace the only one that survives to execution is the one that is read into the interpreter last. This means if you have two functions declared with the same name, in the same scope, and each from a different file the only one that survives is the one in the file that sourced into the HTML page last. There are only two exceptions to this rule.

      The first exception to this rule of last only is if a named object exists, is requested for execution, and then redefined after that execution in a top to bottom evaluation of code fed into the interpreter.

      The second exception is if the code sourced in last exists in a file that is unreachable. Then of course the rule is never applied.

      As a result when more than one file is the cause of namespace confusion the solution is simple. He just has to look for the file included last in the HTML from top to bottom and left to right evaluation of the code by the HTML parser.

      --- In jslint_com@yahoogroups.com, "Jakob Kruse" <kruse@...> wrote:
      >
      > Either I misunderstand you or you misunderstood me.
      >
      > I was talking about namespaces. I'll give an example:
      >
      > In file1.js:
      >
      > var My = {};
      > My.method = function() { ... };
      >
      > In file2.js
      >
      > var My = {};
      > My.other_method = function() { ... };
      >
      > These are two files. Both declare some functionality. They both do so using the same namespace. They way they do so here, if you include them both, you will only get the functionality from the last one.
      >
      > The way you fix this (the simplest one I know of) is:
      >
      > file1.js:
      >
      > var My = My || {};
      > My.method = function() { ... };
      >
      > file2.js:
      >
      > var My = My || {};
      > My.other_method = function() { ... };
      >
      > Now include both files and you will get two methods defined. I fail to see a problem.
      >
      > /Jakob
      >
      > ----- Original Message -----
      > From: sandyhead25
      > [mailto:austin.cheney@...]
      > To: jslint_com@yahoogroups.com
      > Sent: Fri,
      > 29 May 2009 17:06:32 +0200
      > Subject: [jslint] Re: Undefined variable
      >
      >
      > > --- In jslint_com@yahoogroups.com, "Jakob Kruse" <kruse@> wrote:
      > > >
      > > > Hasn't this all gotten a little far from the original problem?
      > > >
      > > > The way I understood it, it had to do with "namespaces". If you have two
      > > javascript files that both declare some functionality in the same namespace,
      > > and you're not sure if you'll be using one file, the other file, or both
      > > files (or you are sure but all three cases exist in various parts of the
      > > application), then you do need "special tricks" (i.e. not just creating the
      > > namespace and putting stuff in it) to avoid the contents of the second file
      > > removing everything declared by the first.
      > > >
      > > > The simple trick is to start both files like this:
      > > >
      > > > var Namespace = Namespace || {};
      > > >
      > > > No need to check if Namespace has already been declared, just use the
      > > rather powerful 'or' operator.
      > > >
      > > > /Jakob
      > > >
      > [snip]
      > >
      > > In that case it is even more simple. If two named values exist within the
      > > same scope the only that survives is the one that is declared last. If it
      > > is a matter of functions competing for the same scope from different JS
      > > files the only one that survives is the one that is in the file sourced last
      > > into the HTML.
      > >
      > >
      >
    • Show all 25 messages in this topic