Javascript 以下节点模块返回";未定义的“;,但是为什么呢?
下面是一个模块,它接受一个参数,一个数字,然后根据该数字是偶数还是奇数,从中添加或减去常量magicNumber。然而,当我运行这段代码时,我得到的只是“未定义”。我做错了什么Javascript 以下节点模块返回";未定义的“;,但是为什么呢?,javascript,return,return-value,node-modules,Javascript,Return,Return Value,Node Modules,下面是一个模块,它接受一个参数,一个数字,然后根据该数字是偶数还是奇数,从中添加或减去常量magicNumber。然而,当我运行这段代码时,我得到的只是“未定义”。我做错了什么 module.exports = (number) => { let answer; //Answer is declared in the global scope const magicNumber = 5; //magicNumber is declared in the global
module.exports = (number) => {
let answer; //Answer is declared in the global scope
const magicNumber = 5; //magicNumber is declared in the global scope
if (number % 2) { //If the number is even
add(number); //run the number through the add function
} else { //otherwise run the number through the subtract function
subtract(number);
}
function add(number){ //Function takes the number as argument, does the math, and returns the value of answer.
answer = number + magicNumber;
return answer;
}
function subtract(number){ //Function takes the number as argument, does the math, and returns the value of answer.
answer = number - magicNumber;
return answer;
}
};
导出的块没有返回任何内容,因此默认情况下它是未定义的
module.exports = (number) => {
let answer; //Answer is declared in the global scope
const magicNumber = 5; //magicNumber is declared in the global scope
if (number % 2) { //If the number is even
return add(number); //you should return here as well
} else { //otherwise run the number through the subtract function
return subtract(number);//you should return here as well
}
function add(number){ //Function takes the number as argument, does the math, and returns the value of answer.
answer = number + magicNumber;
return answer;
}
function subtract(number){ //Function takes the number as argument, does the math, and returns the value of answer.
answer = number - magicNumber;
return answer;
}
};
导出的块没有返回任何内容,因此默认情况下它是未定义的
module.exports = (number) => {
let answer; //Answer is declared in the global scope
const magicNumber = 5; //magicNumber is declared in the global scope
if (number % 2) { //If the number is even
return add(number); //you should return here as well
} else { //otherwise run the number through the subtract function
return subtract(number);//you should return here as well
}
function add(number){ //Function takes the number as argument, does the math, and returns the value of answer.
answer = number + magicNumber;
return answer;
}
function subtract(number){ //Function takes the number as argument, does the math, and returns the value of answer.
answer = number - magicNumber;
return answer;
}
};
您的函数没有返回任何内容,因此默认情况下返回值是
未定义的
(此外,注释需要用/*
/*/
分隔,或者,在一行上,用/
(*
单独使用将不起作用)您需要从函数中返回一些内容,如return add(number)
和返回减法(数字)
很难理解未显示为代码的代码是有意还是无意的(例如,第一行“module.exports…”和最后一行“};”您的问题中没有显示为代码。您的问题还应该包括您用来调用模块的代码。您的函数没有返回任何内容,因此默认情况下返回值是未定义的
(此外,注释需要用/*
/*/
分隔,或者在单行中用/
(*
单独使用是行不通的)您需要从函数中返回一些内容,例如返回add(number);
和返回subtract(number)
很难理解未显示为代码的代码是否是有意的(例如,第一行“module.exports…”和最后一行“};”在您的问题中没有显示为代码。您的问题还应该包括用于调用模块的代码。我的逻辑是,因为一个或另一个函数是add()或subtract(),将不可避免地分别被调用,并且每一行都有自己的返回,代码的预期结果将不可避免地被返回。让我感到困惑的是,返回语句必须在每一步驱动代码的连续执行,而这似乎是直观的,只要其中一行加(数字);或减(数字);如果被命中,这些函数将运行并返回值。我目前正在对此进行研究,但没有发现任何结果。有什么基本的东西我误解了吗?在JavaScript和几乎所有编程语言中,返回语句都是基于块的。我的逻辑是,因为一个或另一个函数,add()或subtract(),将不可避免地分别被调用,并且每一行都有自己的返回,代码的预期结果将不可避免地被返回。让我感到困惑的是,返回语句必须在每一步驱动代码的连续执行,而这似乎是直观的,只要其中一行加(数字);或减(数字);被命中时,这些函数将运行并返回值。我目前正在对此进行研究,但未发现任何结果。我是否误解了一些基本信息?在JavaScript和几乎所有编程语言中,返回语句都是基于块的