I have the following function definitions in the same .js file (call it A.js)
function PParser() {
....
makeExpression = function (lexemes, index) {
return makeNumber(lexemes, index);
}
makeDeclaration = function(lexemes, index)
{
if (lexemes[index].TokenType != LALLPLexer.VAR) {
throw "Expected VAR at index " + index;
}
if (lexemes[index + 1].TokenType != LALLPLexer.ID) {
throw "Expected ID at index " + index + 1;
}
if (lexemes[index + 2].TokenType != LALLPLexer.ASSIGN) {
throw "Expected ASSIGN at index " + index + 2;
}
var expressionNodeResult = makeExpression(lexemes, index + 3);
...
when the “makeExpression” invocation is reached, I was expecting control flow to move to the function defined just above. However, instead, another function named “makeExpression” is called in a completely different .js file (B.js).
function Controller()
{
...
this.parseToStatement = function(statementText)
{
makeExpression = function(expressionNode)
{
return new IntLiteral(expressionNode.Content);
}
try {
statement = parser.parseStatement(new LALLPLexer().lex(statementText));
if (statement.NodeType == LALLPParser.DECLARATION) {
return new Declaration(statement.Id, makeExpression(statement.Expression));
}
}
catch (exception) {
statement = new UnknownStatement(statementText);
}
return statement;
}
}
I’m not sure why. Interestingly enough, the line “parseStatement” shown above is up the call chain from the “makeExpression” invocation. Is this correct javascript behavior and, if so, why should I expect this behavior? How can I get the intended behavior?
I’m not entirely sure about this, but I think js doesn’t make variables local by standard, so all those functions might be attached globally (To the
windowvariable) and hence may overwrite each other.Try adding
varto all of those definitions (orthis.where appropriate). Maybe that will help.