我在类定义中有一个函数,该函数返回undefined
。
这是课程:
class Lexer {
// constructor and another method here
make_tokens() {
var tokens = [];
// checking character values here
console.log(tokens);
// Outputs [ Token { type: 'PLUS', value: undefined }, Token { type: 'PLUS', value: undefined } ] if I enter ++
return tokens, null; // returns undefined, null
}
// make_numbers() goes here
}
搜索答案时,主要是从异步函数返回,但我的函数不是异步的。
我不知道问题出在哪里。
(很抱歉,如果我不能很好地构建问题,但是我是StackOverflow的新手,并且我不知道如何正确构建问题)
答案 0 :(得分:0)
我认为您无法以这种方式返回价值。
class Lexer {
// constructor and another method here
make_tokens() {
var tokens = [];
// checking character values here
console.log(tokens);
// Outputs [ Token { type: 'PLUS', value: undefined }, Token { type: 'PLUS', value: undefined } ] if I enter ++
return [tokens, null]; // returns undefined, null
}
// make_numbers() goes here
}
const lexer = new Lexer();
console.log(lexer.make_tokens());
答案 1 :(得分:0)
您不能返回类似的多个值。
我认为您最终只会返回最后一个值,在这种情况下为null
。
为什么还需要返回null
?
如果您只返回tokens
,它将起作用。
答案 2 :(得分:0)
只需返回tokens
class Lexer {
// constructor and another method here
make_tokens() {
var tokens = [];
// checking character values here
console.log(tokens);
// Outputs [ Token { type: 'PLUS', value: undefined }, Token { type: 'PLUS', value: undefined } ] if I enter ++
return tokens; // returns tokens
}
// make_numbers() goes here
}
const lexer = new Lexer();
console.log(lexer.make_tokens());
答案 3 :(得分:0)
首先,为什么要与其他变量一起返回null,但是如果是这样,则只返回包含2个元素的数组。
这是代码:
credit_id
但这可以是另一种方式。例如,如果不存在令牌,则要返回null。
@Entity({ name: 'trans_credit' })
export class Credit {
@PrimaryColumn({ type: 'int', name: 'credit_id' }) // <- updated the column name
id: number;
@OneToOne(type => Transaction, transaction => transaction.credit)
@JoinColumn({ name: 'trans_id' })
transaction: Transaction;
}