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

536Re: [jslint] Re: Undefined variable

Expand Messages
  • Nagy Endre
    May 27, 2009
      I have multiple js files and I want to add methods to a global namespace.
      What I wanted to achieve is to load the js files independently  with the posibility to augment the namespace  in any of the files.
      So I thought that in every file I will test if I have the namespace, and if not I will create so I can load the files in any order.
      Is this a bad approach? Should I define first, and load only after the others?

      --- On Wed, 5/27/09, Douglas Crockford <douglas@...> wrote:

      --- In jslint_com@yahoogro ups.com, Nagy Endre <forewer2000@ ...> wrote:


      > I'm trying to check if a variable is already defined and define if is not.


      > If (Testvar === undefined) {

      > � var Testvar = {};

      > }


      > JsLint say that Testvar was used before was defined.

      > How can I check this in the right way?

      First, you should avoid using global variables.

      Second, replace "If" with "if". JavaScript is case sensitive.

      Third, the var statement should only be used at the top level of a function or compilation unit. It should not be placed in an if because of hoisting and scope issues.

      So assuming that Testvar is a global variable, say

      if (typeof Testvar === 'undefined') {

      Testvar = {};


      Drop the "var".

      I would also add a hint to JSLint that indicates that you intentionally made a global variable and that this isn't a mistake:

      /*global Testvar*/

      Providing such hints allows JSLint to do a better job for you.

      [Non-text portions of this message have been removed]
    • Show all 25 messages in this topic