【问题标题】:How do I use functions where the parameter is a variable?如何使用参数为变量的函数?
【发布时间】:2015-03-08 17:07:25
【问题描述】:

我一直在做一个小项目来学习 javascript 的基础知识,但我遇到了一个我无法修复的错误。我做了一些研究,但无济于事。 我想要一个产生攻击的程序(作为一个笑话)。它从数组“people”中随机选择一个参数,并将其与数组“offence”中的一个参数连接起来。一切都很顺利,直到我决定将随机发生器变成一个函数。此时它开始做一些奇怪的事情,比如在询问朋友的名字后停止,将 personGenerator 分配给“未定义”。 这是我的代码:

<script>
    //this is plonker base

    //creates a variable that will start the game
    var start = confirm("Are you sure want to participate in plonker base alpha?")

    //starts and loops the game
    if(start==true){
        //asks for another person's name
        var person1 = prompt("Please name one of your best friends.")
    }

    //creates a randomizer function
    var random = function (variable,subject){
        variable = subject[Math.floor(subject.length * Math.random())]
    }

    while(start==true){
        //creates array 'person'
        var person = ["You are ","Your mum is ","Your dad is ", "The world is ", (person1 + " is ")]
        var personGenerator
        random(personGenerator,person)

        //creates an array 'offence'
        var offence = ["an idiot!",
            "a complete pysco!!!",
            "a smelly, worthless peice of junk!",
            "a whale re-incarnated that looks like a squirrel!",
            "a dumb pile of dirt that has the misfortune of seeing itself in the mirror once in a while!",
            "a complete and utter plonker!",
            "a dumbo!",
            "a right dufus!!!",
            "a pile of rabbit dung!",
            "an intelligant, good looking king being... Did I mention - it's opposite day!",
            "a bum-faced rat!!!",
            "a fat, lazy oaf!",
            "a blobfish look-alike!!!!!",
            "a lump of toenail jelly!"]
        var offenceGenerator = offence[Math.floor(offence.length * Math.random())]
        //gives out the offence
        alert(personGenerator + offenceGenerator)
    }
    {
        alert("What a plonker!")
    }
</script>

我是 javascript 新手,所以我不太了解它。请让您的答案易于理解。如果我在任何时候使用了错误的术语,请说出来。

谢谢, 里斯 C.

【问题讨论】:

  • 你不需要做if(start==true)。你可以只做if(start) 因为start 将是一个布尔值。
  • 这段代码有很多语法错误,很多语句末尾缺少分号。
  • 有人告诉我,除非您使用的是旧版本的 javascript,否则您不需要将分号放在末尾?他们会做什么,因为我的大多数 javascript 编码没有它们就可以正常工作......

标签: javascript arrays function variables


【解决方案1】:

这种结构在 Javascript 中不起作用:

//creates a randomizer function
var random = function (variable,subject){
    variable = subject[Math.floor(subject.length * Math.random())]
}

这不会改变传入的变量。相反,您应该从我们的函数中返回新的随机值。

//creates a randomizer function
var random = function (subject){
    return subject[Math.floor(subject.length * Math.random())];
}

然后,你在哪里使用它:

var personGenerator = random(person);

至于为什么您的原始代码在 Javascript 中不起作用,这是因为 Javascript 没有真正的按引用传递,您可以更改原始变量指向的内容。当你这样做时:

//creates a randomizer function
var random = function (variable,subject){
    variable = subject[Math.floor(subject.length * Math.random())]
}

random(personGenerator, person);

您的随机函数中的variable 参数将包含您进行函数调用时personGenerator 变量的内容。但是,它将是一个单独的变量。所以,这样做:

variable = subject[Math.floor(subject.length * Math.random())]

只改变本地函数参数的值。它不会改变personGenerator 的值。

【讨论】:

  • @ReeceC。 - 我在答案的末尾添加了一些解释,说明为什么您的原始代码不起作用。另外,您是否有理由遗漏很多分号?虽然 Javascript 在这方面是允许的,但它不是一个好的做法,在少数情况下可能会导致奇怪的错误。
  • 感谢您的解释 - 现在说得通了!有人告诉我,分号只在旧版本的 js 中有用,现在没有效果。如果他们确实有效果,我从来没有注意到它。请让我知道它们的作用以及如何使用它们(因为我已经很久没有使用它们了,我已经忘记了如何使用它们!)。
  • 一个关于分号主题的google search 会找到很多文章。我一直被这个特定问题困扰:stackoverflow.com/a/1169596/816620,您可以在该问题中看到其他讨论以及各种答案。我个人发现使用分号的代码更具可读性,因为我的大脑立即知道每个语句的结尾在哪里,并且我不必从上下文中弄清楚下一行的语句是否还有更多内容。
  • 好的,谢谢!我将对这个主题做更多的研究。你显然是 js 方面的专家!
猜你喜欢
  • 2018-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-20
  • 1970-01-01
  • 1970-01-01
  • 2012-03-03
相关资源
最近更新 更多