Compartilhar via


A Little More on Nothing

VBScript has Null, Empty and Nothing. What about JScript? Unfortunately, JScript is a little screwed up here.

Like VBScript, in JScript an uninitialized variable is a VT_EMPTY variant. To check to see if a variable is undefined, you can say

typeof(x) == "undefined"

There is a built-in Empty constant like in VBScript -- in JScript it is undefined.

If you say

var x = null;

print(typeof(x));

then you get "object", so you might expect that null is the JScript equivalent of Nothing -- an object reference that refers to no object. From a logical perspective, you'd be right. Unfortunately, for reasons which are lost to the mists of time, JScript implements a null variable internally the same way that VBScript implements a Null variable -- VT_NULL, the "data is missing" value.

This poor decision leads to two problems. First, JScript does not interoperate very well with COM objects which have methods that can legally take a Nothing but not a Null. (I believe that Peter Torr has some addon code that adds this capability to JScript.) Second, JScript processes Null variants passed into it as though they were object references, not database nulls. Thus JScript does not implement any of the rules about Null propagation that VBScript implements.

We corrected these problems somewhat in JScript .NET. JScript .NET uses a null object reference to internally represent null, and can manipulate DBNull objects, so the interop problems go away. However, JScript .NET still does not implement the null propagation semantics for mathematical operations on database nulls.

Comments

  • Anonymous
    October 01, 2003
    Just wanted to say that your blog is far and away the most useful and probably the most interesting blog that I read. Our client application uses IE+DHTML+JScript extensively; we've had to learn a lot of this stuff the hard way. We've learned over time that JScript is an amazingly powerful language, and it is usually a joy to work with, though a compiler/lint/something to help catch spelling errors before runtime would be handy. :-)
  • Anonymous
    October 01, 2003
    Thanks, that's a nice thing to say.If you have any questions or ideas about things you'd like to see in future posts, feel free to send them my way.As for detecting undeclared variables at compile time -- unfortunately in JScript Classic and VBScript that's pretty much impossible due to the way the browser works. Coincidentally, I was planning on posting why exactly that is later this week, so watch this space.Eric
  • Anonymous
    October 01, 2003
    You can always try passing your code through JSC (the JScript .NET compiler) as some sort of "lint" program. You may have to artificially declare all the IE globals you use (like 'window') or you can use /fast-
  • Anonymous
    October 01, 2003
    Eric,I also wanted to tell you how incredibly educational your blog is. It has shot to the top of my "must read" list. Your writing style is great. You really do a nice job explaing technical subject matter in a way that is easy to understand. Keep up the great work!
  • Anonymous
    October 02, 2003
    Thanks Lance. One does one's best.Like I said, if you have any ideas or questions for future posts, feel free to send them my way. (Note though that I have 24 items on my ever-growing list of potential future topics, so I might not get to new ones right away...)
  • Anonymous
    October 14, 2005
    The comment has been removed
  • Anonymous
    April 03, 2006
    Another problem with JScript.Net is that it seems that MS .Net support for script engine (VSA) has limited the typeof usage in JScript.Net.
    For instance,  in JavaScript you can pass undefined argument to typeof without problem:

    print(typeof(x));

    But if same code is run in VSA implemented script engine, you'll get a compilier error. You have to do something
    var x;
    if (typeof(x) != "undefined")
      print(typeof(x));

    In this case what is the point to even have that return value "undefined" if a argument has to be defined or declared first.

    Any comment can send to Jay.Zhang@datamaxx.com