Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. var abc = abc || {}; in Java Script

var abc = abc || {}; in Java Script

Scheduled Pinned Locked Moved JavaScript
javatools
6 Posts 3 Posters 8 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    Umair Feroze
    wrote on last edited by
    #1

    Hi All, I am having a script file which begins with the following statement:

    var ABC = ABC || {};

    This is the very first line of the Java Script file, while the remaining is understandable. Can anyone please tell me what does this line means. I am unable to understand the meaning of it Thanks

    U 1 Reply Last reply
    0
    • U Umair Feroze

      Hi All, I am having a script file which begins with the following statement:

      var ABC = ABC || {};

      This is the very first line of the Java Script file, while the remaining is understandable. Can anyone please tell me what does this line means. I am unable to understand the meaning of it Thanks

      U Offline
      U Offline
      Umair Feroze
      wrote on last edited by
      #2

      Let me reply my Question: The following line creates namespace in Java script:

      var ABC = ABC || {};

      It says if there is a class called ABC create a namespace of it, otherwise create a blank namespace. Hope this may help others for understanding.

      J 1 Reply Last reply
      0
      • U Umair Feroze

        Let me reply my Question: The following line creates namespace in Java script:

        var ABC = ABC || {};

        It says if there is a class called ABC create a namespace of it, otherwise create a blank namespace. Hope this may help others for understanding.

        J Offline
        J Offline
        jsc42
        wrote on last edited by
        #3

        Umair Feroze wrote:

        It says if there is a class called ABC create a namespace of it, otherwise create a blank namespace.

        No, it doesn't create a namespace, because JavaScript doesn't have namespaces; and it does not look for a class as JavaScript does not have classes. It leaves abc as it is if it is an expression whose Boolean value is true e.g. is a non-zero value, a non-empty string, true, a function, or an object (including an array, even if empty). If it is not a true value, it replaces it with an empty object. This is a common idion for defining a variable to hold a context (not a JavaScript concept) without accidentally overwriting it if it is already in use. It is better than simply doing var abc = { }; because any properties in the object will be retained. Technically, it is bad JavaScript because var creates a variable. The appearance of it on the right hand side of an expression when defining it in the left hand side could (in theory) end up using it between the time it is defined and the time it is initialised. I'd rather do ...

        if (typeof abc != 'object') // Use existing abc if it is an object
        var abc = { }; // Create empty abc

        although it is questionable as to whether this is valid or not as vars are 'hitched' so it is equivalent to

        var abc;
        if (typeof abc != 'object')
        abc = { };

        but you can get away with it because varing a variable that already exists in the current scope has no effect.

        N 1 Reply Last reply
        0
        • J jsc42

          Umair Feroze wrote:

          It says if there is a class called ABC create a namespace of it, otherwise create a blank namespace.

          No, it doesn't create a namespace, because JavaScript doesn't have namespaces; and it does not look for a class as JavaScript does not have classes. It leaves abc as it is if it is an expression whose Boolean value is true e.g. is a non-zero value, a non-empty string, true, a function, or an object (including an array, even if empty). If it is not a true value, it replaces it with an empty object. This is a common idion for defining a variable to hold a context (not a JavaScript concept) without accidentally overwriting it if it is already in use. It is better than simply doing var abc = { }; because any properties in the object will be retained. Technically, it is bad JavaScript because var creates a variable. The appearance of it on the right hand side of an expression when defining it in the left hand side could (in theory) end up using it between the time it is defined and the time it is initialised. I'd rather do ...

          if (typeof abc != 'object') // Use existing abc if it is an object
          var abc = { }; // Create empty abc

          although it is questionable as to whether this is valid or not as vars are 'hitched' so it is equivalent to

          var abc;
          if (typeof abc != 'object')
          abc = { };

          but you can get away with it because varing a variable that already exists in the current scope has no effect.

          N Offline
          N Offline
          Not Active
          wrote on last edited by
          #4

          jsc42 wrote:

          because JavaScript doesn't have namespaces

          Not quite correct. Namespaces in JavaScript[^], Namespacing your JavaScript[^], The Importance of Namespace (and a JavaScript solution)[^] plus many other resources.


          I know the language. I've read a book. - _Madmatt

          J 1 Reply Last reply
          0
          • N Not Active

            jsc42 wrote:

            because JavaScript doesn't have namespaces

            Not quite correct. Namespaces in JavaScript[^], Namespacing your JavaScript[^], The Importance of Namespace (and a JavaScript solution)[^] plus many other resources.


            I know the language. I've read a book. - _Madmatt

            J Offline
            J Offline
            jsc42
            wrote on last edited by
            #5

            Defining variables to hold other methods and properties to minimise the pollution of the global scope is a very well established technique. This technique emulates some of the key features of namespaces in other languages. I use this method in my own code and add comments saying that it is creating a namespace; but that does not make it a namespace, any more that if I commented it as being a Space Shuttle would make it a Space Shuttle; they are still just variables that have properties attached to them. Compare, in some fictitious language that has 'real' namespaces:

            namespace Namespace1
            {
            helloWorld: 'Hello, World'
            }

            alert(Namespace1.helloWorld); // Valid

            Namespace1 = false; // Invalid. A namespace is not a variable, so it cannot be given a value

            alert(Namespace1.helloWorld); // Still valid

            against JavaScript:

            /* Cannot use namespace as it is not a keyword
            namespace Namespace1
            */
            // Work around ... use a variable
            var Namespace1 =
            // End of work around
            {
            helloWorld: 'Hello, World'
            }

            alert(Namespace1.helloWorld); // Valid

            Namespace1 = false; // Valid. This is just a a variable, so it can be overwritten

            alert(Namespace1.helloWorld); // Was valid before, but is no longer valid.

            N 1 Reply Last reply
            0
            • J jsc42

              Defining variables to hold other methods and properties to minimise the pollution of the global scope is a very well established technique. This technique emulates some of the key features of namespaces in other languages. I use this method in my own code and add comments saying that it is creating a namespace; but that does not make it a namespace, any more that if I commented it as being a Space Shuttle would make it a Space Shuttle; they are still just variables that have properties attached to them. Compare, in some fictitious language that has 'real' namespaces:

              namespace Namespace1
              {
              helloWorld: 'Hello, World'
              }

              alert(Namespace1.helloWorld); // Valid

              Namespace1 = false; // Invalid. A namespace is not a variable, so it cannot be given a value

              alert(Namespace1.helloWorld); // Still valid

              against JavaScript:

              /* Cannot use namespace as it is not a keyword
              namespace Namespace1
              */
              // Work around ... use a variable
              var Namespace1 =
              // End of work around
              {
              helloWorld: 'Hello, World'
              }

              alert(Namespace1.helloWorld); // Valid

              Namespace1 = false; // Valid. This is just a a variable, so it can be overwritten

              alert(Namespace1.helloWorld); // Was valid before, but is no longer valid.

              N Offline
              N Offline
              Not Active
              wrote on last edited by
              #6

              You are comparing apples and oranges. What is your point? You made the statement "because JavaScript doesn't have namespaces", to which I replied, "Not quite correct". I did not say it does have namespaces, but rather a facsimile within the constraints of the language. To say it does not, or does not support the construct, is false and misleading.


              I know the language. I've read a book. - _Madmatt

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups