No, but you could return an array containing your values:(不,但是您可以返回一个包含您的值的数组:)
function getValues() {
return [getFirstValue(), getSecondValue()];
}
Then you can access them like so:(然后,您可以像这样访问它们:)
var values = getValues();
var first = values[0];
var second = values[1];
With the latest ECMAScript 6 syntax *, you can also destructure the return value more intuitively:(使用最新的ECMAScript 6语法 *,您还可以更直观地分解返回值:)
const [first, second] = getValues();
If you want to put "labels" on each of the returned values (easier to maintain), you can return an object:(如果要将“标签”放在每个返回值上(便于维护),则可以返回一个对象:)
function getValues() {
return {
first: getFirstValue(),
second: getSecondValue(),
};
}
And to access them:(并访问它们:)
var values = getValues();
var first = values.first;
var second = values.second;
Or with ES6 syntax:(或使用ES6语法:)
const {first, second} = getValues();
* See this table for browser compatibility.(*有关浏览器兼容性,请参见下表 。)
Basically, all modern browsers aside from IE support this syntax, but you can compile ES6 code down to IE-compatible JavaScript at build time with tools like Babel .(基本上,除IE之外,所有现代浏览器都支持此语法,但是您可以在构建时使用Babel之类的工具将ES6代码编译为IE兼容的JavaScript。) 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…