catch read / write of undefined 's at runtime

Discussion in 'Javascript' started by Reza Roby, Jan 22, 2004.

  1. Reza Roby

    Reza Roby Guest

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

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

    But this one alerts "undefined":

    function ff()
    var v = 0;
    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?)


    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 Roby, Jan 22, 2004
    1. Advertisements

  2. The code is only a function declaration, the above error will hardly
    occur with the function declaration only but rather when the function is
    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
    needs to be set to
    if you have Mozilla 1.5 or later you can do that after typing
    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, Jan 22, 2004
    1. Advertisements

  3. Reza Roby

    Reza Roby Guest


    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.
    Reza Roby, Jan 22, 2004
  4. Reza Roby

    Brendan Eich Guest

    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.

    Brendan Eich, Jan 22, 2004
  5. 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:
    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.
    No error. Behavior as expected (except from the programmer, who is
    expected not to do that :).
    Lasse Reichstein Nielsen, Jan 22, 2004
  6. Reza Roby

    Reza Roby Guest

    Thanks Lasse. That completely clarifies things.
    Reza Roby, Jan 22, 2004
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.