Javascript 功能将不起作用

Javascript function will not work

每次我 运行 脚本,它都会说:

party is undefined

这是我得到的:

party()

var party = function (name){
    alert("You must find your way into the party and kill the VIP")
};

它不会 运行 在任何 js 网站上。我该怎么办?

您在 var party = function (na...

中定义之前调用了 party()

只需先定义它,然后调用它,如下所示:

var party = function (name) {alert("You must find your way into the party and kill the VIP")};
party()

要么是@user148098 建议的,要么:

party()

function party(){
    alert("You must find your way into the party and kill the VIP")};

明确定义函数的位置。

问题可能是因为您需要在调用它之前定义一个函数。这是一个例子:

var party = function (name) {alert("You must find your way into the party and kill the VIP")};

party()

此外,您可能没有在脚本中嵌入适当的标签来获得它 运行。如果我只是将前面的代码放入一个文件并将其保存为 html 它不会 运行 因为我没有将它放在标签中。请参阅下面的确切代码:

<script>
 var party = function (name) {alert("You must find your way into the party and kill the VIP")};

    party()
 
</script>

希望对您有所帮助!

您必须在使用前定义您的函数。