Reputation: 9620
I'm new to JavaScript. In nashorn 1.8.0_11 I see the behavior below. Note print(x)
works fine yet evaluating x
causes a crash. May I consider this a bug? If so, is it a known bug?
jjs> var x = Object.create(null);
jjs> print(x);
<shell>:1 TypeError: Cannot get default string value
jjs> x;
Exception in thread "main" ECMAScript Exception: TypeError: Cannot get default string value
at jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:56)
at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:212)
at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:184)
at jdk.nashorn.internal.objects.Global.getDefaultValue(Global.java:592)
at jdk.nashorn.internal.runtime.ScriptObject.getDefaultValue(ScriptObject.java:1257)
at jdk.nashorn.internal.runtime.JSType.toPrimitive(JSType.java:256)
at jdk.nashorn.internal.runtime.JSType.toPrimitive(JSType.java:252)
at jdk.nashorn.internal.runtime.JSType.toStringImpl(JSType.java:993)
at jdk.nashorn.internal.runtime.JSType.toString(JSType.java:326)
at jdk.nashorn.tools.Shell.readEvalPrint(Shell.java:449)
at jdk.nashorn.tools.Shell.run(Shell.java:155)
at jdk.nashorn.tools.Shell.main(Shell.java:130)
at jdk.nashorn.tools.Shell.main(Shell.java:109)
Upvotes: 1
Views: 831
Reputation: 4405
This is as expected. When you evaluate expressions interactively with "jjs" shell tool, it converts evaluated expression result as String to print the same. Also, "print" function calls toString on object to print string representation of it to console. With Object.create(null), you are creating an object whose prototype is null (and hence does not inherit Object.prototype.toString). Also, your object does not have "toString" property with function typed value and hence the TypeError. Please note that you can similar behaviour with v8 shell as well.
V8 version 3.25.15 [sample shell]
var x = Object.create(null) x; print(x)
Upvotes: 1