Syntax for variables within a namespace

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gimme_this_gimme_that@yahoo.com

    #1

    Syntax for variables within a namespace


    var BN = {
    foo: function() {
    alert("hello world");
    },
    z: function() {
    alert("hello world");
    },
    }

    I would like to add a member moo to BN whose definition is

    var moo = {"a":"A","b","B "};

    How do I do it?

    Thanks

  • Lee

    #2
    Re: Syntax for variables within a namespace

    gimme_this_gimm e_that@yahoo.co m said:
    >
    >
    >var BN = {
    foo: function() {
    alert("hello world");
    },
    z: function() {
    alert("hello world");
    },
    >}
    >
    >I would like to add a member moo to BN whose definition is
    >
    >var moo = {"a":"A","b","B "};
    >
    >How do I do it?
    The right hand side of your assignment to moo isn't valid Javascript syntax, so
    there is no way to add it to BN. What are you really trying to do?


    --

    Comment

    • gimme_this_gimme_that@yahoo.com

      #3
      Re: Syntax for variables within a namespace


      moo is a hash.
      var moo = {"a":"A","b":"B "};

      Comment

      • RobG

        #4
        Re: Syntax for variables within a namespace

        On Sep 12, 6:27 am, "gimme_this_gim me_t...@yahoo.c om"
        <gimme_this_gim me_t...@yahoo.c omwrote:
        var BN = {
        foo: function() {
        alert("hello world");
        },
        z: function() {
        alert("hello world");
        },
        Variable names starting with capital letters are usually reserved for
        constructors. That last comma is a syntax error.

        }
        >
        I would like to add a member moo to BN whose definition is
        >
        var moo = {"a":"A","b","B "};
        Another stray comma, and property names don't need to be quoted in
        object literals:

        var moo = {a:"A", b:"B"};

        How do I do it?
        To add a property moo to the object BN:

        BN.moo = {...};


        --
        Rob

        Comment

        • gimme_this_gimme_that@yahoo.com

          #5
          Re: Syntax for variables within a namespace

          Can you make the assignment withing the braces?

          As in

          BN = {
          // create moo here
          }


          Comment

          • Thomas 'PointedEars' Lahn

            #6
            Re: Syntax for variables within a namespace

            RobG wrote:
            On Sep 12, 6:27 am, "gimme_this_gim me_t...@yahoo.c om"
            <gimme_this_gim me_t...@yahoo.c omwrote:
            >var BN = {
            > foo: function() {
            > alert("hello world");
            > },
            > z: function() {
            > alert("hello world");
            > },
            >
            Variable names starting with capital letters are usually reserved for
            constructors.
            ACK
            That last comma is a syntax error.
            Strictly adhering to the ECMA-262 Ed. 3 grammar, yes. Considering the
            Conformance section of ECMA-262 (all editions so far), no.
            [...] property names don't need to be quoted in object literals:
            Not quite correct.
            var moo = {a:"A", b:"B"};
            It is only possible to omit the quote characters or apostrophes around
            the property name here because `a' and `b' are identifiers.


            PointedEars
            --
            realism: HTML 4.01 Strict
            evangelism: XHTML 1.0 Strict
            madness: XHTML 1.1 as application/xhtml+xml
            -- Bjoern Hoehrmann

            Comment

            • gimme_this_gimme_that@yahoo.com

              #7
              Re: Syntax for variables within a namespace

              Thanks.

              Comment

              • Douglas Crockford

                #8
                Re: Syntax for variables within a namespace

                gimme_this_gimm e_that@yahoo.co m wrote:
                var BN = {
                foo: function() {
                alert("hello world");
                },
                z: function() {
                alert("hello world");
                },
                }
                >
                I would like to add a member moo to BN whose definition is
                >
                var moo = {"a":"A","b","B "};
                var BN = {
                foo: function() {
                alert("hello world");
                },
                z: function() {
                alert("hello world");
                },
                moo: {"a": "A", "b": "B"}
                }

                or you could have augmented your original BN:

                BN.moo = {"a": "A", "b": "B"};


                Comment

                • Gregor Kofler

                  #9
                  Re: Syntax for variables within a namespace

                  Thomas 'PointedEars' Lahn meinte:
                  >That last comma is a syntax error.
                  >
                  Strictly adhering to the ECMA-262 Ed. 3 grammar, yes. Considering the
                  Conformance section of ECMA-262 (all editions so far), no.
                  One should note, that IE (6) has it's problems with this extra comma. FF
                  and Opera (and suppose others) not.

                  Gregor


                  --
                  http://www.gregorkofler.at ::: Landschafts- und Reisefotografie
                  http://www.licht-blick.at ::: Forum für Multivisionsvor träge
                  http://www.image2d.com ::: Bildagentur für den alpinen Raum

                  Comment

                  • John G Harris

                    #10
                    Re: Syntax for variables within a namespace

                    On Tue, 11 Sep 2007 at 23:33:31, in comp.lang.javas cript, Thomas
                    'PointedEars' Lahn wrote:
                    >RobG wrote:
                    <snip>
                    >That last comma is a syntax error.
                    >
                    >Strictly adhering to the ECMA-262 Ed. 3 grammar, yes. Considering the
                    >Conformance section of ECMA-262 (all editions so far), no.
                    <snip>

                    The Conformance section is there to permit extra features. It's not
                    there to permit random typing errors. A certain amount of common sense
                    is assumed, of browser designers as well as of web designers.

                    John
                    --
                    John Harris

                    Comment

                    Working...