为什么这个&&不是短路?

时间:2014-11-12 23:50:51

标签: javascript node.js templatizer

此代码来自节点模块templatizer

if (i === 3 && node.type === "ExpressionStatement" && node.expression.callee.object.name === "buf" &&
       node.expression.arguments.length === 1 && node.expression.arguments[0].type === "Literal") {

       // save the simple string
       simpleString = node.expression.arguments[0].value;
       cnt++;
    }

node.type的值是'VariableDeclaration',因此逻辑表达式为false,因此node.expression甚至不应该被评估,但它似乎是......

  

TypeError:无法读取未定义的属性“object”       at /node_modules/templatizer/lib/simplifyTemplate.js:34:89       在Array.forEach(native)

这意味着'被叫'中的'对象'。 expressioncallee都未定义。一旦我跨过条件节点崩溃。

修改

我认为Javascript工作正常,也许一些异步代码导致调试器出现奇怪的结果。如果我在循环的顶部放置一个console.log,它有时会提供有意义的输出......

/usr/local/bin/node bin/www
count: 1
i:0
node.type: VariableDeclaration
expression: undefined
node.expression.callee: undefined

count: 2
i:1
node.type: VariableDeclaration
expression: undefined
node.expression.callee: undefined

count: 3
i:2
node.type: VariableDeclaration
expression: undefined
node.expression.callee: undefined

count: 4
i:3
node.type: ExpressionStatement
expression: [object Object]
node.expression.callee: [object Object]

count: 5
i:4
node.type: ReturnStatement
expression: undefined
node.expression.callee: undefined

count: 6
i:0
node.type: VariableDeclaration
expression: undefined
node.expression.callee: undefined

count: 7
i:1
node.type: VariableDeclaration
expression: undefined
node.expression.callee: undefined

count: 8
i:2
node.type: VariableDeclaration
expression: undefined
node.expression.callee: undefined

count: 9
i:3
node.type: ExpressionStatement
expression: [object Object]
node.expression.callee: undefined


/Users/ME/WebstormProjects/MySite/node_modules/templatizer/lib/simplifyTemplate.js:41
= 3 && node.type === "ExpressionStatement" && node.expression.callee.object.na
                                                                    ^
TypeError: Cannot read property 'object' of undefined
    at /Users/ME/WebstormProjects/MySite/node_modules/templatizer/lib/simplifyTemplate.js:41:89
    at Array.forEach (native)
    at module.exports (/Users/ME/WebstormProjects/MySite/node_modules/templatizer/lib/simplifyTemplate.js:15:18)
    at /Users/ME/WebstormProjects/MySite/node_modules/templatizer/templatizer.js:111:20
    at Array.forEach (native)
    at module.exports (/Users/ME/WebstormProjects/MySite/node_modules/templatizer/templatizer.js:95:15)
    at Object.<anonymous> (/Users/ME/WebstormProjects/MySite/app.js:23:1)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)

Process finished with exit code 8

所以看起来像模块中的一个错误。

1 个答案:

答案 0 :(得分:0)

似乎调试器显示的是执行的不同部分的值而不是导致崩溃的值。当然它必须在某处异步运行。这是模块中的一个错误。临时修复是为条件添加额外的检查。

if (i === 3 && node.type === "ExpressionStatement" && node.expression.callee && node.expression.callee.object.name === "buf" &&
                node.expression.arguments.length === 1 && node.expression.arguments[0].type === "Literal") 

我打开了issue