catch read / write of undefined 's at runtime

R

Reza Roby

The following code reports a runtime error "v has no properties."

function ff()
{
var v;
v.x=5; //error
alert(v.x);
}

But this one alerts "undefined":

function ff()
{
var v = 0;
v.x=5;
alert(v.x); //alert "undefined"
}

while generating no runtime warnings at all.

Is there a way to force javascript to report this type of error (in the
javascript console?)

PS:

I understand sometimes people may want to read an undefined, as a valid
value. However, it would be _very_ good if the rest of us can at least
get runtime warnings for that.

Thank you again for your help.

Reza.
 
M

Martin Honnen

Reza said:
The following code reports a runtime error "v has no properties."

function ff()
{
var v;
v.x=5; //error
alert(v.x);
}

The code is only a function declaration, the above error will hardly
occur with the function declaration only but rather when the function is
called.
But this one alerts "undefined":

function ff()
{
var v = 0;
v.x=5;
alert(v.x); //alert "undefined"
}

while generating no runtime warnings at all.

Is there a way to force javascript to report this type of error (in the
javascript console?)

Mozilla can warn you

Warning: reference to undefined property v.x

if you set your preferences for the JavaScript console to generate
warnings. There is no error happening there so "to report this type of
error" would be wrong as there is no error.
The preference
javascript.options.strict
needs to be set to
true
if you have Mozilla 1.5 or later you can do that after typing
about:config
in the location, displaying the preferences and then right-clicking on
the above preference to change its value to a boolean true.
Or look up how to find and edit your preferences file
 
R

Reza Roby

Martin said:
The code is only a function declaration, the above error will hardly
occur with the function declaration only but rather when the function is
called.



Mozilla can warn you

Warning: reference to undefined property v.x

if you set your preferences for the JavaScript console to generate
warnings. There is no error happening there so "to report this type of
error" would be wrong as there is no error.



Martin,

Thank you. That's exactly what I wanted. It works perfectly.

You brought up another question though: Is there a particular reason why
this is not considered an error? Since the line v.x=5 is dropped
without any effects(??) on program state, it seems that the JS engine is
"thinking" of this as an error. Does that make sense?

And thanks again.
 
B

Brendan Eich

Reza said:
Is there a way to force javascript to report this type of error (in the
javascript console?)


In Mozilla, yes. Set the javascript.options.strict preference, or from
script in a web page, the options.strict property, to true.

You're posting to the wrong newsgroup (n.p.m.seamonkey) -- see
followup-to: header in this post.

/be
 
L

Lasse Reichstein Nielsen

Reza Roby said:
You brought up another question though: Is there a particular reason
why this is not considered an error? Since the line v.x=5 is dropped
without any effects(??) on program state,

When you write
expression . propertyName
Javascript first evaluates the expression to a value and then tries to
find the property in it. If the value is not an object, it is
automatically converted to one if possible (using the appropriate
constructor for the value's type - Number, String or Boolean, and
null and undefined fails).

That is what makes it possible to write:
"foo".length
It is equivalent to:
new String(foo).length
which works because String objects have a length property.

So, what you write is either an error, because v is undefined and v.x
fails to convert v to an object, or equivalent to

var v = 0;
new Number(v).x = 5;
alert(new Number(v).x); // alert undefined

As you can see, the two objects created are different. Setting the "x"
property on the first does not affect the second.
it seems that the JS engine is "thinking" of this as an error.

No error. Behavior as expected (except from the programmer, who is
expected not to do that :).
 
R

Reza Roby

Lasse said:
You brought up another question though: Is there a particular reason
why this is not considered an error? Since the line v.x=5 is dropped
without any effects(??) on program state,

[...]
So, what you write is either an error, because v is undefined and v.x
fails to convert v to an object, or equivalent to

var v = 0;
new Number(v).x = 5;
alert(new Number(v).x); // alert undefined

As you can see, the two objects created are different. Setting the "x"
property on the first does not affect the second.


Thanks Lasse. That completely clarifies things.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top