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

537RE: [jslint] Re: Undefined variable

Expand Messages
  • Jakob Kruse
    May 27, 2009
      my_namespace = my_namespace || {};

      I use Prototype and have added this to my collection of utilities:

      var Namespace = {
      separator: ".",

      create: function (namespace) {
      return namespace.split(Namespace.separator).inject(window,
      function (parent, child) {
      return (parent[child] = parent[child] || {});

      add: function (namespace, contents) {
      return Object.extend(Namespace.create(namespace), contents);

      Probably doesn’t make too much sense if you’re not used to Prototype, but the essence of it is the first line above.


      From: jslint_com@yahoogroups.com [mailto:jslint_com@yahoogroups.com] On Behalf Of Nagy Endre
      Sent: 27. maj 2009 20:06
      To: jslint_com@yahoogroups.com
      Subject: Re: [jslint] Re: Undefined variable

      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]

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