[英]Returning from a function returns undefined
I have a function in a class definition that returns undefined
.我在返回
undefined
的 class 定义中有一个 function 。 Here is the class:这是 class:
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
}
When I searched for answers, I mainly got returning from asynchronous functions but my function is not asynchronous.当我搜索答案时,我主要从异步函数返回,但我的 function 不是异步的。
I do not know what the problem is.我不知道问题是什么。
(sorry if I didn't structure it well but I am new to StackOverflow and I don't know how to structure a question properly) (对不起,如果我没有很好地构造它,但我是 StackOverflow 的新手,我不知道如何正确地构造问题)
i don't think you could return value that way.我认为您不能以这种方式返回值。
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());
You can't return multiple values like that.你不能像这样返回多个值。
I think you end up just returning the last value which in this case is null
.我认为你最终只是返回最后一个值,在这种情况下是
null
。
Why do you need to return null
aswell?为什么还需要退回
null
?
If you just return tokens
it will work.如果您只是返回
tokens
,它将起作用。
Just return tokens
只需返回
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());
Firstly, why you want to return null with other variable but if so then just return an array consisting of 2 elements.首先,为什么要返回 null 和其他变量,但如果是这样,那么只需返回一个由 2 个元素组成的数组。
Here's is the code:这是代码:
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
}
But it can be the other way.但它可以是另一种方式。 For eg You want to return null if tokens are not present.
例如,如果令牌不存在,您想返回 null。
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.