473,499 Members | 1,648 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

catch read / write of undefined 's at runtime

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.
Jul 20 '05 #1
5 2144


Reza Roby wrote:
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
--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2
Martin Honnen wrote:


Reza Roby wrote:
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.


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

Jul 20 '05 #3
Reza Roby wrote:
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

Jul 20 '05 #4
Reza Roby <sp*************@parvan.net> writes:
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 :).
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5
Lasse Reichstein Nielsen wrote:
Reza Roby <sp*************@parvan.net> writes:

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.


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 :).

Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3357
by: valued customer | last post by:
Is there a more concise way to do something like the the desired code below? The gripe is with the try-catch syntax. It takes *way* too many lines of code to evaluate a conditional expression...
22
3215
by: Amali | last post by:
I'm newdie in c programming. this is my first project in programming. I have to write a program for a airline reservation. this is what i have done yet. but when it runs it shows the number of...
3
2507
by: Martin | last post by:
Hi. I need to identify the type of the exception in the universal handler (catch (...)) for debugging purposes. Point is that I write a testing console application, which must call some...
4
4327
by: =?Utf-8?B?UmljaA==?= | last post by:
I have to read data from an Excel Sheet. Using Microsoft Office Interop and Automation I create an Excel object Dim xl As New Excel.Application, wkbk As Excel.Workbook, rng as Excel.Range I...
0
7009
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7223
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7390
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4602
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.