473,581 Members | 2,786 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 2153


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.opti ons.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.opti ons.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.opti ons.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.seamonke y) -- 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".lengt h
It is equivalent to:
new String(foo).len gth
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/rasterTriangleD OM.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
3368
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 when zero or more parts of the conditional expression may trigger an error. In this case, the trigger is a call to a non-defined (null) object. In...
22
3242
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 seats as 0 and the flight no. is also repeating. If any can tell why is this please help me. #include<stdio.h> #include<ctype.h> #include<conio.h>
3
2514
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 functions from a DLL. One of these functions throws an exception, and since my small application knows nothing about the exception type system of that DLL...
4
4336
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 open a workbook and then read the data from a worksheet using the Sheet's UsedRange property into an Excel Range object I call rng. Then I loop...
0
7808
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7914
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8181
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6564
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3809
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3835
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2309
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 we have to send another system
1
1410
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1145
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.