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

P

Peter Michaux

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
 
R

RobG

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.
 
R

ron.h.hall

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top