简体   繁体   English

Javascript-尝试将函数分配给变量

[英]Javascript - Trying to assign a function to a variable

I'm just writing a very simple function in javascript to calculate a factorial. 我只是在用javascript写一个非常简单的函数来计算阶乘。 I understand that in javascript you can assign a function to a variable. 我了解您可以在javascript中将函数分配给变量。

So I have tried this on an online compiler ( https://repl.it/languages/javascript ) and this is what my code looks like 所以我已经在在线编译器( https://repl.it/languages/javascript )上尝试过了,这就是我的代码的样子

var mynum = prompt("Enter a number", "<enter a number>");
var answer;
if (isNaN(mynum)){
 console.log(mynum +" is not a number");
}
else{
 console.log("You entered "+mynum);
 answer = function (mynum){
            var i = mynum-1;
            var temp = mynum;
            while(i>0){
              temp = temp*i;
              i--;
            }
            return temp;
  };
 console.log("the factorial of "+mynum+" is "+answer);
}

But when I run this the output keeps including the whole function as "answer" 但是,当我运行此命令时,输出将包括整个功能,并将其作为“答案”

You entered 23
the factorial of 23 is function (mynum) {var _loopStart = Date.now(),_loopIt =     0;
var i = mynum - 1;
var temp = mynum;setTimeout(function () {_loopStart = Infinity;});
while (i > 0) {if (++_loopIt > 5000 && Date.now() - _loopStart > 150) throw new RangeError("Potential infinite loop. You can disable this from settings.");
  temp = temp * i;
  i--;
}
return temp;

} }

However i don't have this issue when i create the function and then call it separately (something like answer = function(mynum). 但是当我创建函数然后分别调用它时,我没有这个问题(类似于答案= function(mynum)。

Can anyone please let me know why this is happening? 谁能让我知道为什么会这样吗?

Thanks! 谢谢!

Assigning a function to a variable is different from assigning its evaluation. 将函数分配给变量与分配其评估值不同。

In your case, you have two solutions available : 对于您的情况,您有两种可用的解决方案:

Make an effective call to your assigned function at logging time: 在记录时对分配的函数进行有效调用:

console.log("the factorial of "+mynum+" is "+answer(mynum));

Make an effective call to your assigned function at assignation time: 在分配时间对分配的功能进行有效调用:

answer = (function (mynum){
  var i = mynum-1;
  var temp = mynum;
  while(i > 0) {
    temp = temp*i;
    i--;
  }
  return temp;
}(mynum));

Both solutions are quite equivalent for your specific situation. 两种解决方案在您的特定情况下都相当。

Why? 为什么?

Because declaring a function like so: 因为这样声明一个函数:

var func = function () {
  console.log("Hello!");
};

Or like so: 或者像这样:

function func () {
  console.log("Hello!");
};

Has little difference 没有什么区别

As pointed out, you have to call it as a function. 如前所述,您必须将其作为函数来调用。

var mynum = prompt("Enter a number", "<enter a number>");
var answer;
if (isNaN(mynum)){
 console.log(mynum +" is not a number");
}
else{
 console.log("You entered "+mynum);
 answer = function (mynum){
            var i = mynum-1;
            var temp = mynum;
            while(i>0){
              temp = temp*i;
              i--;
            }
            return temp;
  };
 console.log("the factorial of "+mynum+" is "+answer (mynum));
}

Alternatively, you could use IIEF(mmediately invoked function expression): 或者,您可以使用IIEF(立即调用的函数表达式):

var mynum = prompt("Enter a number", "<enter a number>");
var answer;
if (isNaN(mynum)){
 console.log(mynum +" is not a number");
}
else{
 console.log("You entered "+mynum);
 answer = (function (mynum){
            var i = mynum-1;
            var temp = mynum;
            while(i>0){
              temp = temp*i;
              i--;
            }
            return temp;
  })(mynum);
 console.log("the factorial of "+mynum+" is "+answer);
}

Note that I've added a parenthesis around your function and passed in arguments. 请注意,我已经在函数周围添加了括号并传递了参数。 That's how you can immediately invoke functions. 这样便可以立即调用函数。

When you return a function in javascript such as answer() . 当您使用javascript返回诸如answer()的函数时。 You must call it as such. 您必须这样称呼它。

console.log("the factorial of "+mynum+" is "+answer(mynum)); console.log(“” + mynum +“的阶乘是” + answer(mynum));

You need to use the function that you wrote by calling it ie 您需要使用通过调用它编写的函数,即

function add (a,b) {
  return a + b;
}

add(1,2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM