Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Javascript / Client Side Development

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


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

On May 13, 6:05 am, Peter Michaux <petermich@gmail.com> 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.

On May 12, 1:05 pm, Peter Michaux <petermich@gmail.com> 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

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc