Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6346732

can't assign undefined or null value to JavaScript global variables

XMLWordPrintable

    • b63
    • generic
    • generic

      JavaScript has two special values 'null' and 'undefined' respectively. It should be possible to assign these values for global (or local) variables. This can be seen by following jrunscript sessions:

      ./jrunscript
      js> var v = null
      js> v
      script error: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "v" is not defined. (<STDIN>#1) in <STDIN> at line number 1
      js>quit()

      ./jrunscript
      js> var u = undefined
      js> u
      script error: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "u" is not defined. (<STDIN>#1) in <STDIN> at line number 1
      js>quit()
      In fact, these are allowed for local variables as shown by following jrunscript sessions:

      ./jrunscript
      js> function f() { var v = null; print(v); }
      js> f()
      null
      js>quit()

      ./jrunscript
      js> function g() { var u = undefined; print(u); }
      js> g()
      undefined
      js>quit()

      These are problems with API implementation rather than jrunscript tool. This can be verified by a simple program as below:

      import javax.script.*;

      public class t {
              public static void main(String[] args) throws Exception {
                      ScriptEngineManager m = new ScriptEngineManager();
                      ScriptEngine e = m.getEngineByName("js");
                      e.eval("x = null; print(x)");
              }
      }


      or

      import javax.script.*;

      public class t {
              public static void main(String[] args) throws Exception {
                      ScriptEngineManager m = new ScriptEngineManager();
                      ScriptEngine e = m.getEngineByName("js");
                      e.eval("x = undefined; print(x);");
              }
      }


      Exception in thread "main" javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "x" is not defined. (<Unknown source>#1) in <Unknown source> at line number 1

      is thrown with these programs.

            sundar Sundararajan Athijegannathan
            sundar Sundararajan Athijegannathan
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: