【问题标题】:JavaScript, how to call a functionJavaScript,如何调用函数
【发布时间】:2016-07-18 22:14:53
【问题描述】:

这可能听起来很愚蠢,比如“这家伙在尝试吗?”愚蠢的。但我正在拼命地尝试。我搜索的所有内容都只向我展示了如何编写函数,而不是回调它。

var choice1 = function(){
    var choice = 5
        while(choice != "n" || "s") {
            document.write("<p>There's a door norh of which you sit, south is a small window with a slight breeze coming through</p>");
            document.write("<p> north: door  south: window</p>");
            choice = prompt("north or south");
            if(choice == "n" || "south"){
                document.write("<p>You stand up, aching in your joints you grunt. Inspecting the door you see, what seems to be, a large iron grated cell door? ''Why would I be in a cell?'' you think to yourself.</p>")
                break
            }else if(choice == "s" || "south"){
                document.write("<p>You stand up, aching in your joints you grunt. Inspectig the window</p>");
                break
            }else if(choice != "n" || "s" || "noth" || "south") {
                document.write("<p>Please pick one of the paths</p>");
        };
    };
};

function choice1()

这就是我所拥有的,它可以工作,因为我已经测试了不在函数中但我无法让它调用函数。

【问题讨论】:

  • 只写choice1(),而不是function choice() 第二次写这篇文章。
  • 当您编写function choice1() 时,您所做的基本上是声明另一个名为choice1() 的函数
  • 真的不擅长搜索吗? MDN's Guide On JavaScript FunctionsDefiningCalling
  • choice != "n" || "s" 应该是choice != "n" || choice != "s"
  • @Caleb:不要因反对票和反对意见而气馁。可能很难成为 SO 的绝对初学者。请浏览其他人提供的信息并在 MDN 上阅读更多信息。

标签: javascript function call invoke


【解决方案1】:
choice1()

键入function 表示您要声明一个函数而不是使用一个函数。

【讨论】:

    【解决方案2】:

    将最后一行 function choice1() 替换为:

    choice1()
    

    【讨论】:

    • 我尝试搜索,我得到的唯一一件事是如何编写函数以及如何使用函数进行数学运算的示例。对不起,下次我会努力成为一个失败者
    【解决方案3】:

    要调用该函数,只需:

    choice1(); 
    

    【讨论】:

    • 抱歉@user104317 不清楚您的意思是什么?我的答案是如何调用函数,正如 OP 所要求的那样......
    • @AmirTaboul 在你之前说了同样的话,但做了更好的解释
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-07
    • 2018-08-15
    • 2012-07-04
    • 2020-09-27
    • 2014-03-23
    相关资源
    最近更新 更多