An object's constructor name as a string. Can definitely be determined?

D

Douglas Crockford

VK said:
JSLint is not the script engine, so it's allowed to have different
opinions on different topics ;-)

That is true, but it is irrelevant. If you ran all of your snippets thru JSLint
before posting to this forum, your reputation would be significantly less tarnished.
But I have to reconsider my previous statement:
<script type="text/javascript">
function() {}
</script>
is not a FunctionDeclaration: it is still FunctionExpression. Here we
have "expression without assignment" wich is totally valid byt rather
useless.

It is a syntax error. Your example isn't just useless. It is wrong.
 
M

Michael Winter

VK said:
Exactly: and this rule (which is especially true for javascript
engines) is violated on Opera 9.

It is a principle, not a rule. That said, it's not one that I would want
to apply to a programming or scripting language.
The rule is that a javascript program may error out only if no
available interpretations left: this is why even utterly wrong
scripts are "working" sometimes - but often not in the intended way.

Which is precisely why I'd prefer it not happen with programming
languages. Better to have a program (or better yet, a compiler, where
applicable) raise an exception and stop executing than guessing and
acting unpredictably.

[snip]

Mike
 
R

Richard Cornford

VK said:
Exactly: and this rule (which is especially true for javascript
engines) is violated on Opera 9.

It all goes straight over your head, doesn't it? You have complete
control over the code you write, but you would rather blame a web
browser for doing no more than it claims to do than learn how to take
control of the code you write.
The rule is that a javascript program
may error out only if no available interpretations left: this is why
even utterly wrong scripts are "working" sometimes -

Finally a subject where you have some experience.
but often not in the intended way.


The exact parsing algorithm was given by you in this thread,

No it was not.
as well as two possible branches of this algorithm.

I certainly mentioned that the only two applicable target constructs
were either Statements or Function Declarations.
<script type="text/javascript">
function(){}
</script>

1) Found "function" keyword left hand side, searching for identifier.

There is no sense in "left hand side". the keyword would be the next
token in a sequence where all preceding tokens (if any) were accounted
for.
2) If an identifier found then this is FunctionDeclaration;

It is not a FunctionDeclaration unless it fully satisfies the
production rules for FunctionDeclaration.
if not then go to step 3
3) No identifier found, left hand side anonymous function.

Anonymous functions don't have sides. And is Function Declaration is
excluded the target construct is now Statement.
If Opera 9
then go to step 4, otherwise go to step 5
4) Opera 9: "booh, booh... bad syntax... :-("

Or, you now know you have messed up and can do something about fixing
your code.

(Also, you are assuming that Opera 9 is the only browser that will
apply the syntax rules in accordance with the specification. It almost
certainly is not, and indeed it would be more rational to assume that
all browsers would apply the language's syntax rules and then be
surprised at the few that are tolerant of this type of error).
4) Others: function expression w/o assignment, seems useless
but not our damn business.

And now you have someone who thinks that what they have done is
correct, and will probably be going on to repeat their mistake on the
grounds that it "works", even though it is then no more than a mystical
incantation repeated because it has been used in code that "works" in
the past and has not directly caused problems (at least under
superficial testing).

I don't need to tell you anything about programming by mystical
incantation, or superficial testing, as you are certainly the expert in
those areas.
This way Opera 9 between i)trying to further interpret the code and ii)
error out is choosing to error out, which is not OK for a reliable
script engine.

It is that sort of thinking that results in the code you write, which
is anything but reliable.

Richard.
 
V

VK

Hah! The entire Co in one spot (though the posting order hierarchy is
not correct IMHO. Richard?)

OK, so much easy to answer:

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script type="text/javascript">

function(){} + 2 == 'Learn JavaScript better, guys';

// And do not take single erroneus implementation
// for some standard.
</script>
</head>
<body>
</body>
</html>

P.S. Damn German beer is good! I'm getting sad just thinking of coming
back to Bud-water in a few weeks. :-(
:)
 
R

Richard Cornford

VK wrote:
OK, so much easy to answer:
function(){} + 2 == 'Learn JavaScript better, guys';

Another syntax error and another pointless operation (if it were not a
syntax error). So what is your point?
// And do not take single erroneus implementation
// for some standard.
<snip>

Who said Opera was the only browser reporting the syntax error? Your
question was whether there were any browsers which had a problem with
your syntax error, and if one can be named that question has been
answered. There is no need for any single browser to be taken as
defining a standard when a standard already exists and is unambiguous
on the subject.

Richard.
 
V

VK

Richard said:
Who said Opera was the only browser reporting the syntax error?

Do you have any other one besides Opera 9? I have no idea about Safari,
but I would like to dismiss it right away (in case if): if you take a
dring every time this UA does something right with scripting, you'll
end up thirsty and angry :)

Any other mainstream browser?

A good hint: if you're in dubts about something, check it on Netscape
4.7 In 99% of cases whatever it does is what was really intended, if it
contradicts to ECMAScript specs it simply means that the free-lancers
failed to describe properly what they've seen.

One more time: there is not syntax error here, but there is Opera's
parser laziness: instead of further interprete the source and to
understand that this is an expression w/o assignment: instead of that
it just errors out on the first opportunity.
 
J

John G Harris

<script type="text/javascript">
function(){}
</script>

1) Found "function" keyword left hand side, searching for identifier.
2) If an identifier found then this is FunctionDeclaration; if not then
go to step 3
3) No identifier found, left hand side anonymous function.
<snip>

Obviously the programmer has forgotten to type something like
this.a =
at the beginning of the line, or has forgotten to type the function name
in the middle of the line. A halfway decent compiler will say
!!!ERROR!!!
so the programmer sees and corrects the mistake straight away.

If the compiler says nothing the programmer has to write to
comp.lang.javascript saying it doesn't work, then spend hours
complaining that no-one has replied quickly enough, and then complain
again when they ask to see more code because he thinks the error is
somewhere else, which it isn't.

Unfortunately, some people get paid for the time needed to find obscure
errors in their code. They get paid less if the error is made too
obvious, so they scream loudly when the error is highlighted straight
away.

John
 
V

VK

1) Found "function" keyword left hand side, searching for identifier.
Obviously the programmer has forgotten to type something like
this.a =
at the beginning of the line, or has forgotten to type the function name
in the middle of the line. A halfway decent compiler will say
!!!ERROR!!!
so the programmer sees and corrects the mistake straight away.

Obviously I will never hire you for an application development :-(
because you have a holy mix of that a parser/engine is and what a
tidy-like suggestive checker is.

The engine's job is not to enforce some "preferred style" of
programming or some "best practice" onto developers. It has much more
humble but much more technically difficult task: from each segment of
the source code try to produce an execution code non-violating given
border criterias. That is not damn parser business what is this code
for and why did one use it this way.
Tidy-like checkers are free to declare ++i or --i harmful or declare
void function(){};
as an error (an opinion shared by no one real engine). Parser has not
such freedom: it has to eat whatever fed until chokes.
 
R

Richard Cornford

VK said:
Do you have any other one besides Opera 9?

Yes. Not reporting this syntax error as a syntax error appears to be the
exception (unsurprisingly).
I have no idea about Safari,
but I would like to dismiss it right away

Which brings us back to what I said earlier. You write code without
recognising a syntax error in the language you are using and then when
your scripts only work in a couple of browsers at most you blame the
browsers. That the fault here is yours rather than the browser's is
obvious as soon as you realise that people exist who do manage to write
effective code for Safari. You think they are doing that by putting
disproportionate effort into the task, but it is more likely that they
are just presenting Safari with code devoid of syntax errors, mystical
incantations and your other nonsense, and the result just works.
(in case if): if you take a dring every time this
UA does something right with
scripting, you'll end up thirsty and angry :)

The VK definition of what is "right" is too detached from reality for
that to be a criteria for judging anything.
Any other mainstream browser?

Earlier today it was "Any ECMAScript-compliant engine erroring out on the
below, please", but now that it should be obvious that
ECMAScript-compliant browsers are likely to generate syntax errors with
code that contains syntax errors you want to retreat to your 'safe'
ground of "mainstream browsers". Which presumably is "mainstream
browsers" as defined by VK, and so pretty much Windows IE and Firefox
only.
A good hint: if you're in dubts about something, check it
on Netscape 4.7
Idiot.

In 99% of cases whatever it does is what was really
intended,

Unlikely as Netscape 4.7 implemented ECMA 262 2nd Edition (with
extensions) and your "mainstream browsers" (and other current scriptable
browsers) implement ECMA 262, 3rd Edition.
if it contradicts to ECMAScript specs it simply means
that the free-lancers

Have you looked at the list of the people who wrote ECMA 262?
failed to describe properly what they've seen.

With the contributors to ECMA 262 having direct access to the source code
for both JavaScript(tm) and JScript it is unlikely that observation
determined their behaviour.
One more time: there is not syntax error here,

Only when "here" is restricted to the contents of your deranged mind.
but there is Opera's parser laziness:

It is not lazy to report a syntax error when one is seen.
instead of further interprete the source and to
understand that this is an expression w/o assignment:

There is nothing in the language's syntax that allows such an
interpretation, and you cannot assume that any browser that does not
report the syntax error is making such an interpretation. As far as you
know they are just recognising the futility of the construct and just
filtering it out. Indeed experimentation suggests that the syntax error
is being filtered out rather than being interpreted as a Function
Expression. Try:-

<html>
<head>
<title></title>
<script type="text/javascript">
var a;

function(){alert('I was recognised 1');}

(a);

var b = function(){alert('I was recognised 2');}

(a);

</script>
</head>
<body>
</body>
</html>

- in Firefox and IE 6 and you see "I was recognised 2" alerted by the
second function expression but no sign of 'I was recognised 1', which
would be the consequences of the - function(){alert('I was recognised
1');} - line being interpreted as a function expression, as can be
illustrated by wrapping that line in parenthesise to give:-

<html>
<head>
<title></title>
<script type="text/javascript">
var a;

(function(){alert('I was recognised 1');})

(a);

var b = function(){alert('I was recognised 2');}

(a);

</script>
</head>
<body>
</body>
</html>

- where the absence of any syntax error allows both 'I was recognised 1'
and 'I was recognised 2' to be alerted.

So " interprete the source and to understand that this is an expression
w/o assignment" is not what Firefox or IE are doing with the syntax
error. Instead they appear to be silently ignoring it. And silently
ignoring syntax errors is less useful than reporting them.
instead of that
it just errors out on the first opportunity.

Which tells the programmer at the earliest point possible that they have
made an error.

Obviously you would not appreciate that, as you don't like being told
that you have made an error, and prefer not to recognise them when you
have. But then that is one of the things makes you by far the worst
programmer I have ever encountered.

Richard.
 
J

John G Harris

VK said:
Obviously I will never hire you for an application development :-(
because you have a holy mix of that a parser/engine is and what a
tidy-like suggestive checker is.

You obviously don't know what a parser does. It's a pattern matcher. A
match tells the compiler which syntax rule the code matches. Then the
compiler knows exactly what the programmer wants the program to do.

For instance, if the parser is given
var x = 42;
the parser finds that the code matches the syntax rule for creating a
variable named x with the initial value 42.

On the other hand, if the parser is given
x var 42 =;
the parser doesn't find a match with any of the hundreds of syntax rules
so the compiler doesn't know what the programmer was thinking.

In VK's universe the browser's AI plugin would come to life at this
point and say that it believes the programmer meant ... well, what?

The engine's job is not to enforce some "preferred style" of
programming or some "best practice" onto developers. It has much more
humble but much more technically difficult task: from each segment of
the source code try to produce an execution code non-violating given
border criterias. That is not damn parser business what is this code
for and why did one use it this way.
Tidy-like checkers are free to declare ++i or --i harmful or declare
void function(){};
as an error (an opinion shared by no one real engine). Parser has not
such freedom: it has to eat whatever fed until chokes.

VK is confused once again. The parser's job is to see which syntax rule
the code matches. It's a style guide or lint program that's expected to
say it's dangerous to use 'with'. Thus the parser must declare that
; ++i; --i; void function() {};
are all legal since each matches a syntax rule. A style guide might say
that i++ is preferred to ++i, or that void is rarely needed, but it
doesn't have to say that.

Does javascript have syntax rules? Yes : they're written in ECMA 262.
Does every javascript programmer have to be able to read them? No, but
if they can't they mustn't tell people what the rules are or aren't.

John

Postscript :
No syntax rule matches
; function() {};
but there is one that matches
; void function() {};
Strange, but true.
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,177
Latest member
OrderGlucea
Top