มันเป็นเพราะวิธีการที่ REPL ประเมินค่าอินพุตซึ่งท้ายที่สุดก็คือ:
(hi)()
วงเล็บเพิ่มเติมถูกเพิ่มเพื่อบังคับให้เป็นExpression :
// First we attempt to eval as expression with parens.
// This catches '{a : 1}' properly.
self.eval('(' + evalCmd + ')',
// ...
เจตนาคือการรักษาที่{...}
เป็นObject
ตัวอักษร / initialisersมากกว่าที่จะเป็นบล็อก
var stmt = '{ "foo": "bar" }';
var expr = '(' + stmt + ')';
console.log(eval(expr)); // Object {foo: "bar"}
console.log(eval(stmt)); // SyntaxError: Unexpected token :
และดังที่ leesei ได้กล่าวถึงสิ่งนี้ได้ถูกเปลี่ยนเป็น 0.11.x ซึ่งจะถูก{ ... }
รวมเข้าด้วยกันแทนที่จะป้อนทั้งหมด:
if (/^\s*\{/.test(evalCmd) && /\}\s*$/.test(evalCmd)) {
// It's confusing for `{ a : 1 }` to be interpreted as a block
// statement rather than an object literal. So, we first try
// to wrap it in parentheses, so that it will be interpreted as
// an expression.
evalCmd = '(' + evalCmd + ')\n';
} else {
// otherwise we just append a \n so that it will be either
// terminated, or continued onto the next expression if it's an
// unexpected end of input.
evalCmd = evalCmd + '\n';
}