new Foo(); as a statement? (ECMAScript spec question)

Discussion in 'Javascript' started by Peter Michaux, May 12, 2007.

  1. Hi,

    I am reading the ECMAScript specs trying to figure out if the next
    line is a legal statement or not

    new Foo();

    I think the above code may only be legal as an expression and not as a
    stand alone statement. Would this make the above a bug?

    Douglas Crockford's JSLint will choke on the above line of code and
    stop parsing. All the browsers seem to accept it as ok and work as I
    expect: the returned object just doesn't get assigned to anything.

    The time I have used a line like the above is when the constructor has
    side effects and the "class" keeps track of all its instances.

    Any ideas what is right or wrong in this case?

    Thanks,
    Peter
     
    Peter Michaux, May 12, 2007
    #1
    1. Advertising

  2. Peter Michaux

    RobG Guest

    On May 13, 6:05 am, Peter Michaux <> wrote:
    > Hi,
    >
    > I am reading the ECMAScript specs trying to figure out if the next
    > line is a legal statement or not
    >
    > new Foo();
    >
    > I think the above code may only be legal as an expression and not as a
    > stand alone statement. Would this make the above a bug?


    I can't see the point in agonising over the difference between an
    expression and a statement - it's moot.

    e.g. who cares whether the following is an exression or a statement?

    foo && foo();


    > Douglas Crockford's JSLint will choke on the above line of code and
    > stop parsing. All the browsers seem to accept it as ok and work as I
    > expect: the returned object just doesn't get assigned to anything.


    JSLint is a *verifier*, not just a validator, so it looks for errors
    beyond straight language validity to where it thinks you might have
    made an error.


    > The time I have used a line like the above is when the constructor has
    > side effects and the "class" keeps track of all its instances.
    >
    > Any ideas what is right or wrong in this case?


    Seems OK to me, but I'd question why you are using a constructor this
    way - there are usually many ways to achieve the same result. "Right"
    or "wrong" can only really be judged in context, "better" or "best"
    could be more appropriate terms.


    --
    Rob.
     
    RobG, May 13, 2007
    #2
    1. Advertising

  3. Peter Michaux

    Guest

    On May 12, 1:05 pm, Peter Michaux <> wrote:
    > Hi,
    >
    > I am reading the ECMAScript specs trying to figure out if the next
    > line is a legal statement or not
    >
    > new Foo();
    >
    > I think the above code may only be legal as an expression and not as a
    > stand alone statement. Would this make the above a bug?
    >

    <...>
    >
    > Any ideas what is right or wrong in this case?
    >


    ECMA-262/3 12.4 should help with the answer.

    --
    .../rh
     
    , May 13, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert Allan Schwartz

    new foo -vs- new foo()

    Robert Allan Schwartz, Aug 11, 2004, in forum: C++
    Replies:
    2
    Views:
    337
    marbac
    Aug 12, 2004
  2. Replies:
    3
    Views:
    383
    John Roth
    Jul 29, 2005
  3. .rhavin grobert

    vector: Foo[5] == ((foo*)Foo) + 5 ?

    .rhavin grobert, Sep 23, 2008, in forum: C++
    Replies:
    4
    Views:
    394
    JaredGrubb
    Sep 24, 2008
  4. Replies:
    4
    Views:
    146
    Thomas 'PointedEars' Lahn
    Dec 23, 2007
  5. dhtml
    Replies:
    32
    Views:
    421
    Peter Michaux
    Oct 13, 2008
Loading...

Share This Page