简体   繁体   English

Javascript-Math.random作为参数

[英]Javascript - Math.random as parameter

I asked this before, but a little vague and poorly worded. 我之前曾问过这个问题,但是有点含糊和措辞不佳。

I have a object that takes in Action s and puts them into a stack that it goes through over time. 我有一个对象,它接受Action ,并将它们放到随着时间流逝而经过的堆栈中。 It performs the first Action in the stack until it's done, then it performs the second, and so on. 它执行堆栈中的第一个Action直到完成,然后执行第二个,依此类推。 There is a RepeatAction that can take in an array of other Action s and perform them a number of times in a similar fashion. 有一个RepeatAction可以接受其他Action的数组,并以类似的方式执行多次。 ( .repeat() simply puts a new RepeatAction into the objects stack. .repeat()只是将新的RepeatAction放入对象堆栈中。

Take this for example: 以这个为例:

object.repeat(10,[new LogAction(Math.random())]);

Given that LogAction only takes in a parameter and logs it out. 鉴于LogAction仅接受参数并将其注销。 When the object's repeat function gets called it will put 10 LogAction s into its stack, but they will all log the same number. 调用对象的repeat函数时,它将把10 LogAction放入其堆栈,但是它们都将记录相同的数字。 What I'm looking for is a way that it will log a different number all 10 times. 我正在寻找的是一种将10次记录不同数字的方式。

You may say just pass in Math.random as a function, but then what if I want to pass in 4 * Math.random() ? 您可能会说只是将Math.random作为函数传递,但是如果我想传递4 * Math.random()怎么办?

Any help? 有什么帮助吗?

You may say just pass in Math.random as a function, 您可能会说只是将Math.random作为函数传递,

Yes, I would. 是的,我会。

but then what if I want to pass in 4 * Math.random()? 但是如果我想传递4 * Math.random()怎么办?

Then in place of Math.random , use 然后代替Math.random ,使用

function() { return 4 * Math.random(); }

The code needs to invoke a function (later) to get a different value. 该代码需要调用一个函数(以后)以获得不同的值。 eg 例如

// pass in the `random` function - do not get a random number immediately!
object.repeat(10, [new LogAction(Math.random)])

// create and pass in a custom function that uses `random` itself
var rand4 = function () { return Math.random() * 4 }
object.repeat(10, [new LogAction(rand4)])

Since "LogAction" is not disclosed, I'll make a simple example to work with the above. 由于未公开“ LogAction”,因此我将举一个简单的示例来进行上述操作。

function LogAction (arg) {
    this.execute = function () {
        // If the supplied argument was a function, evaluate it
        // to get the value to log. Otherwise, log what was supplied.
        var value = (typeof arg === 'function') ? arg() : arg;
        console.log(value);                
    }
}

Reading through How do JavaScript closures work? 通读JavaScript闭包如何工作? will likely increase the appreciation of functions as first-class values, which the above relies upon. 以上将依赖于对作为一流价值的功能的重视。

You can pass function which returns result of random: 您可以传递返回随机结果的函数:

object.repeat(10,[new LogAction(function(){return Math.random();})]);

And in LogAction function you simply need to check if argument is a function: 在LogAction函数中,您只需要检查参数是否为函数:

function LogAction(arg){
    var value = arg&& getType.toString.call(arg) === '[object Function]' ? arg() : arg;

    //log value    
}

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

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