简体   繁体   English

JS:为什么为变量分配一个函数而不是调用该函数?

[英]JS: Why is my variable being assigned a function instead of calling the function?

I'm trying to set some random coordinates on a map. 我正在尝试在地图上设置一些随机坐标。 I wrote the console.log line to see if it was successfully picking numbers, but it's telling me that everything in my destinations array is NaN. 我写了console.log行,看它是否成功选择了数字,但这是告诉我destinations数组中的所有内容都是NaN。

It looks like it's assigning the Math.random function to the variable instead of calling Math.random and giving it the number it returns. 看起来好像是将Math.random函数分配给变量,而不是调用Math.random并给它返回的数字。 Anyone know what's up? 有人知道怎么回事吗?

var nodeCount = 10
var mapSize = 100;

var destinations = new Array(nodeCount);
for (var i = 0; i < nodeCount; i++) {
    destinations[i] = new Array(2);
}

var generateDestinations = function(nodeCount) {
    for (var i=0; i<nodeCount; i++) {
        destinations[i][0] = Math.floor(Math.random*mapSize); //sets x-coord
        destinations[i][1] = Math.floor(Math.random*mapSize); //sets y-coord
        console.log(destinations);
    }
};
generateDestinations(nodeCount);

Math.random() is a function, so it needs to be invoked with the call-operator: Math.random()是一个函数,因此需要使用call-operator进行调用:

Math.floor(Math.random() * mapSize);
//                    ^^

Same goes for the line below it. 下面的行也一样。


Moreover, you can use the array literal syntax instead of calling the constructor. 此外,您可以使用数组文字语法而不是调用构造函数。 It's much cleaner and more expressive: 它更干净,更富有表现力:

var destinations = [];
for (var i = 0; i < nodeCount; i++) {
    destinations[i] = [];
}

http://www.w3schools.com/jsref/jsref_random.asp http://www.w3schools.com/jsref/jsref_random.asp

try this, looks like your defining a max value but not a min, (I am assuming you want a number between 0 and mapSize) 试试看,看起来像您定义的是一个最大值,而不是一个最小值,(我假设您想要一个介于0和mapSize之间的数字)

    //                         \/ max value
    Math.floor((Math.random()*mapSize) + 0);
    //                                   /\ min value

also Math.random is a function so its 也是Math.random是一个函数,所以它

Math.random()

为什么我的JS function 只操作一个<div>分配给 class 而不是全部?</div><div id="text_translate"><p> 我有这个 JS function 应该最初隐藏一个具有 class 名称“tw”的,当点击一个按钮时它应该使它可见。 但是,每当我单击按钮时,它只会更改一个 div 的可见性。 我有 4 个。我该如何解决这个问题?</p><pre> function myFunction(){ var elms = document.getElementsByClassName("tw"); Array.from(elms).forEach((x) =&gt; { if (x.style.display === "block") { x.style.display = "none"; } else { x.style.display = "block"; } }) }</pre><p> <a href="https://jsfiddle.net/qm8bxryh/307/" rel="nofollow noreferrer">https://jsfiddle.net/qm8bxryh/307/</a>这是小提琴</p></div> - Why does my JS function only manipulate one <div> that is assigned to the class instead of all?

暂无
暂无

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

相关问题 为什么我的变量没有从 JS function 获得赋值? - Why my variable not getting assigned value from JS function? 在Node.js中的函数中调用分配给变量的函数 - Calling a function assigned to a variable within a function in Node.js 为什么我的JS function 只操作一个<div>分配给 class 而不是全部?</div><div id="text_translate"><p> 我有这个 JS function 应该最初隐藏一个具有 class 名称“tw”的,当点击一个按钮时它应该使它可见。 但是,每当我单击按钮时,它只会更改一个 div 的可见性。 我有 4 个。我该如何解决这个问题?</p><pre> function myFunction(){ var elms = document.getElementsByClassName("tw"); Array.from(elms).forEach((x) =&gt; { if (x.style.display === "block") { x.style.display = "none"; } else { x.style.display = "block"; } }) }</pre><p> <a href="https://jsfiddle.net/qm8bxryh/307/" rel="nofollow noreferrer">https://jsfiddle.net/qm8bxryh/307/</a>这是小提琴</p></div> - Why does my JS function only manipulate one <div> that is assigned to the class instead of all? 全局变量未在函数外部分配。 - Global Variable is not being assigned outside function. 变量未从 function 中赋值 - Variable not being assigned value out of function 当分配给变量的函数直接放在自调用匿名函数上方时,为什么要执行? - Why is a function assigned to a variable executing when placed directly above a self-calling anonymous function? 为什么我的区间调用我的 function 即使它在一个变量中? - Why is my interval calling my function even though its in a variable? 为什么我的Javascript变量没有在此函数之外设置? - Why is my Javascript variable not being set outside of this function? 为什么在此函数外部看不到对变量的更改? - Why aren't the changes to my variable being visible outside of this function? 为什么我的JavaScript函数看到mouseevent而不是空变量? - Why does my JavaScript function see a mouseevent instead of an empty variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM