简体   繁体   English

将方法作为参数传递给类中的另一个方法时出错

[英]Got error on passing method as parameter on another method inside class

 class Parser { private tokens: Array<ITokenized> private token_index: number; private current_token: ITokenized | undefined; constructor(tokens: Array<ITokenized>) { // console.log(tokens); this.tokens = tokens this.token_index = -1; this.current_token = undefined this.next() } next() { this.token_index += 1; if (this.token_index < this.tokens.length) { this.current_token = this.tokens[this.token_index] } return this.current_token } public parse(): any { let result = this.expression(); return result; } private factor() { let token = this.current_token if ([TOK_INT, TOK_FLOAT].includes(token?.dataType)) { this.next(); return new NumberNode(token?.value).represent(); } } private term() { return this.binaryOperation(this.factor, [TOK_MULTI, TOK_DIVI]) } private expression() { return this.binaryOperation(this.term, [TOK_PLUS, TOK_MINUS]) } public binaryOperation(func: Function, operator: Array<string>) { let leftNode, operationToken, rightNode; leftNode = func() while (operator.includes(this.current_token?.dataType)) { operationToken = this.current_token; this.next(); rightNode = func() leftNode = new BinaryOperator(leftNode, operationToken?.dataType, rightNode).represent(); } return leftNode; } } export default Parser;

F:\\Programming-Files\\hello-world\\dev-projects\\puCpp-programming-language\\src\\parser\\Parser.ts:69 return this.binaryOperation(this.factor, [TOK_MULTI, TOK_DIVI]) ^ TypeError: Cannot read property 'binaryOperation' of undefined at Parser.term (F:\\Programming-Files\\hello-world\\dev-projects\\puCpp-programming-language\\src\\parser\\Parser.ts:69:15) at Parser.binaryOperation (F:\\Programming-Files\\hello-world\\dev-projects\\puCpp-programming-language\\src\\parser\\Parser.ts:78:14) at Parser.expression (F:\\Programming-Files\\hello-world\\dev-projects\\puCpp-programming-language\\src\\parser\\Parser.ts:73:15) at Parser.parse (F:\\Programming-Files\\hello-world\\dev-projects\\puCpp-programming-language\\src\\parser\\Parser.ts:56:21) at Runner.start (F:\\Programming-Files\\hello-world\\dev-projects\\puCpp-programming-language\\src\\lexer\\Runner.ts:21:26) at F:\\Programming-Files\\hello-world\\dev-projects\\puCpp-programming-language\\src\\index.ts:25:46 at Interface._onLine (readline.js:306:5) at Interface._line (readline.js:656 F:\\Programming-Files\\hello-world\\dev-projects\\puCpp-programming-language\\src\\parser\\Parser.ts:69 return this.binaryOperation(this.factor, [TOK_MULTI, TOK_DIVI]) ^ TypeError: 无法读取Parser.term (F:\\Programming-Files\\hello-world\\dev-projects\\puCpp-programming-language\\src\\parser\\Parser.ts:69:15) at Parser.binaryOperation (F :\\Programming-Files\\hello-world\\dev-projects\\puCpp-programming-language\\src\\parser\\Parser.ts:78:14) 在 Parser.expression (F:\\Programming-Files\\hello-world\\dev-项目\\puCpp-programming-language\\src\\parser\\Parser.ts:73:15) 在 Parser.parse (F:\\Programming-Files\\hello-world\\dev-projects\\puCpp-programming-language\\src\\parser\\ Parser.ts:56:21) 在 Runner.start (F:\\Programming-Files\\hello-world\\dev-projects\\puCpp-programming-language\\src\\lexer\\Runner.ts:21:26) 在 F:\\ Programming-Files\\hello-world\\dev-projects\\puCpp-programming-language\\src\\index.ts:25:46 在 Interface._onLine (readline.js:306:5) 在 Interface._line (readline.js:656 :8) at Interface._ttyWrite (readline.js:937:14) at Socket.onkeypress (readline.js:184:10) :8) 在 Interface._ttyWrite (readline.js:937:14) 在 Socket.onkeypress (readline.js:184:10)

Try explicitly binding your function arguments:尝试显式绑定您的函数参数:

private term() {
        return this.binaryOperation(this.factor.bind(this), [TOK_MULTI, TOK_DIVI])
    }

private expression() {
        return this.binaryOperation(this.term.bind(this), [TOK_PLUS, TOK_MINUS])
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM