Javascript 从d3.random返回数字

Javascript 从d3.random返回数字,javascript,d3.js,higher-order-functions,Javascript,D3.js,Higher Order Functions,我想询问变量/函数的返回类型。我有一些这样的示例代码: var randomX = d3.randomNormal(mean, 80), randomY = d3.randomNormal(mean, 80), points = d3.range(1000).map(function() {return [randomX(), randomY()]; }); randomX和randomY被声明为变量,为什么在return行中它们是带括号的randomX(

我想询问变量/函数的返回类型。我有一些这样的示例代码:

var randomX = d3.randomNormal(mean, 80),      
    randomY = d3.randomNormal(mean, 80),     
    points = d3.range(1000).map(function() {return [randomX(), randomY()]; });
randomX
randomY
被声明为变量,为什么在
return
行中它们是带括号的
randomX()
randomY()


我是Javascript新手,请帮帮我。提前谢谢你

我相信其他地方已经回答了这个问题,但看看这个例子:

function fn1(inputNumber) {
    return function () {
        return Math.round(Math.random() * inputNumber)
    }
}

var bigRand = fn1(10000);
console.log(bigRand());
console.log(bigRand());
这有点像d3.0的功能。您正在通过闭包创建一个新函数,在本例中,主要是为了避免重复相同的输入。这实际上与执行以下操作相同:

function fn2(inputNumber) {
    return Math.round(Math.random() * inputNumber)
}

console.log(fn2(10000));
console.log(fn2(10000));

请参见

我确信其他地方已经回答了这个问题,但请看一看这个例子:

function fn1(inputNumber) {
    return function () {
        return Math.round(Math.random() * inputNumber)
    }
}

var bigRand = fn1(10000);
console.log(bigRand());
console.log(bigRand());
这有点像d3.0的功能。您正在通过闭包创建一个新函数,在本例中,主要是为了避免重复相同的输入。这实际上与执行以下操作相同:

function fn2(inputNumber) {
    return Math.round(Math.random() * inputNumber)
}

console.log(fn2(10000));
console.log(fn2(10000));

请参见

将特定的D3信息添加到注释和已接受的答案中(这很好地涵盖了问题):
D3.randomNormal()
返回一个函数

问题很清楚:

返回用于生成正态(高斯)分布的随机数的函数。(强调矿山)

如果检查源代码,也可以看到这一点:

export default (function sourceRandomNormal(source) {
  function randomNormal(mu, sigma) {
      //bunch of code here...
  };
  return randomNormal;//returns the function here!
});
因此,如果你想得到实际的数字,你必须调用它(带括号):

让我们证明一下

首先,不带括号:

d3.randomNormal([mu][, sigma])()
//parentheses here------------^
var random=d3.randomNormal(10,1);
控制台日志(随机)

只需将特定的D3信息添加到注释和已接受的答案中(这很好地涵盖了问题):
D3.randomNormal()
返回一个函数

问题很清楚:

返回用于生成正态(高斯)分布的随机数的函数。(强调矿山)

如果检查源代码,也可以看到这一点:

export default (function sourceRandomNormal(source) {
  function randomNormal(mu, sigma) {
      //bunch of code here...
  };
  return randomNormal;//returns the function here!
});
因此,如果你想得到实际的数字,你必须调用它(带括号):

让我们证明一下

首先,不带括号:

d3.randomNormal([mu][, sigma])()
//parentheses here------------^
var random=d3.randomNormal(10,1);
控制台日志(随机)

这意味着它们属于
函数
类型
随机正常
必须返回函数,而不是数字。谢谢。当我们再次调用该函数时,实际的随机数出现了,对吗?(我的意思是,randomX()是否返回一个随机数?@iomagenta我们不能确定。什么是
d3
?看起来好像是这样。好吧,我明白了。D3是一个JS库,用于根据数据操作文档。这意味着它们属于
函数
类型
randomNormal
必须返回函数,而不是数字。谢谢。当我们再次调用该函数时,实际的随机数出现了,对吗?(我的意思是,randomX()是否返回一个随机数?@iomagenta我们不能确定。什么是
d3
?看起来好像是这样。好吧,我明白了。D3是一个JS库,用于基于数据操作文档。