--- old/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptRuntime.java 2015-06-15 14:38:29.461421878 +0200 +++ new/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptRuntime.java 2015-06-15 14:38:29.381421880 +0200 @@ -373,9 +373,14 @@ * @return prototype object after merge */ public static ScriptObject mergeScope(final ScriptObject scope) { - final ScriptObject global = scope.getProto(); - global.addBoundProperties(scope); - return global; + final ScriptObject proto = scope.getProto(); + // Variable declarations go into the first parent scope that is not a with-statement + ScriptObject parentScope = proto; + while (parentScope instanceof WithObject) { + parentScope = parentScope.getProto(); + } + parentScope.addBoundProperties(scope); + return proto; } /**