为什么在JS中交换函数调用后不能立即显示数组?为什么函数调用的结果是未定义的?
const swap = (arr) => {
for (let i = 0; i < arr.length - 1; i++) {
var tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
}
}
r = [...Array(10)].map( (_, i) => (i + 1) * 3);
console.log(r); // --> Array [ 3, 6, 9, 12, 15, 18, 21, 24, 27, 30 ]
//swap(r);
// why array can't be displayed immediately after swap function call?
console.log( swap(r) ); // --> displays undefined but the swap function worked out
console.log(r); // --> Array [ 6, 9, 12, 15, 18, 21, 24, 27, 30, 3 ]
&#13;
swap(r);
console.log(r);
&#13;
工作正常。
答案 0 :(得分:1)
是的,你是对的,因为你试图输出swap
函数调用的结果,但你没有返回任何具体的东西。所以默认结果是未定义的。
如果你想在交换后输出数组,那么在你的函数中返回它,如下所示:
const swap = (arr) => {
for (let i = 0; i < arr.length - 1; i++) {
var tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
}
return arr;
}
r = [...Array(10)].map((_, i) => (i + 1) * 3);
console.log(swap(r));
&#13;
最终由您来决定您希望该功能如何工作。如您所知,只要您不尝试使用函数调用的结果,就不需要返回任何内容。
关于它为什么不自动返回数组,你必须记住一个函数可以为许多对象做很多不同的事情。没有办法知道应该自动返回哪个对象或值,因此尝试做任何类似魔法的事情都没有意义。如果您想要退货,请确保退货。
答案 1 :(得分:1)
您不需要返回任何内容,并保留您的函数swap
。传递的数组是r = [...Array(10)].map((_, i) => (i + 1) * 3)
的引用,因此,每个修改都会在函数swap
结束后修改该数组。
只需打印数组console.log(r);
。
const swap = (arr) => {
for (let i = 0; i < arr.length - 1; i++) {
var tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
}
}
r = [...Array(10)].map((_, i) => (i + 1) * 3);
console.log(r); // --> Array [ 3, 6, 9, 12, 15, 18, 21, 24, 27, 30 ]
swap(r);
// why array can't be displayed immediately after swap function call?
// console.log(swap(r)); // --> displays undefined but the swap function worked out
console.log(r); // --> Array [ 6, 9, 12, 15, 18, 21, 24, 27, 30, 3 ]
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 2 :(得分:1)
是的,你是对的;你应该使用public class ApplicationContextService implements ApplicationContextAware {
private static ApplicationContext appContext;
private static long lastFailedDbConnectTime = 0;
private static long checkConnectionTimeframe = 1000 * 30;// 1 min frame before the next check
public static ApplicationContext getApplicationContext() {
return appContext;
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
appContext = applicationContext;
}
public static void setApplicationContextStatic(ApplicationContext ac) {
appContext = ac;
}
public static <T> T getBean(Class<T> clazz) {
return appContext.getBean(clazz);
}
@SuppressWarnings("unchecked")
public static <T> T getBean(String beanId) {
return (T) appContext.getBean(beanId);
}
public static boolean isMainDataBaseAvailable() {
try {
if ((System.currentTimeMillis() - lastFailedDbConnectTime) < checkConnectionTimeframe) {
return false;
}
HikariDataSource hikariCPMain = (HikariDataSource) appContext.getBean("HikariCPMain");
long timeout = hikariCPMain.getConnectionTimeout();
hikariCPMain.setConnectionTimeout(9500);
hikariCPMain.getConnection().close();
hikariCPMain.setConnectionTimeout(timeout);//restore default DB timeout
return true;
} catch (Exception e) {
Logger.error("[ApplicationContextService] Exception in isMainDataBaseAvailable method. Cannot connect to main database.", e, false);
//HikariDataSource hikariCPAlternative = (HikariDataSource) appContext.getBean("HikariCPAlternative");
lastFailedDbConnectTime = System.currentTimeMillis();
return false;
}
}
public static HikariDataSource getHikariDataSource() {
return (HikariDataSource) appContext.getBean(ApplicationContextService.isMainDataBaseAvailable() ? "HikariCPMain" : "HikariCPAlternative");
}
,但我认为你想要的是旋转阵列。
return arr;