简体   繁体   English

JavaScript 类型问题:如何将 function 的返回值分配给变量,而不是 function 本身?

[英]JavaScript Type Problem: How do I assign the return value of a function to a variable, but not the function itself?

I am currently working with RPG maker MZ and try to write my first plugin.我目前正在与 RPG 制造商 MZ 合作并尝试编写我的第一个插件。 Now i do have not much experience with JavaScript, only with Java, and i think this error arises from the weaker typing system compared to java. I think that the source of the problem is, that my var filteredList gets a function as its value where it actually should had actually gotten the return value of the function. so my question would be: how do i assign filteredList the correct value?现在我对 JavaScript 没有太多经验,只有 Java,我认为这个错误是由于与 java 相比较弱的打字系统引起的。我认为问题的根源是,我的 var filteredList 得到一个 function 作为它的值在哪里它实际上应该得到 function 的返回值。所以我的问题是:如何为 filteredList 分配正确的值?

In the game, the function was supposed to replace the standard way of determine Loot from enemies.在游戏中,function 应该取代确定敌人战利品的标准方法。 The code with the problem is the following:有问题的代码如下:


 let actualDropList = new Array;
    commonItemList.forEach(matchedElement => {
            commonItemDataList.push($dataItems[matchedElement]);
            console.log($dataItems[matchedElement]);
} 
    });
    let CommonDropWorth = this.enemy().meta.HellsCommonDropBase /*+ (enemy.dataObject.meta.HellsCommonDropFlat * (this.enemy.level-1))*/;
    var filteredList =  commonItemDataList.filter((Item => Item.price <= CommonDropWorth));
    var cleanFilteredList = function(){
        return commonItemDataList.filter((Item => Item.price <= CommonDropWorth));

    };
    while (filteredList.length > 0){
        let item;
        if (filteredList.length > 1){
            item = filteredList[Math.floor(Math.random()*filteredList.length)];
            CommonDropWorth = CommonDropWorth - item.price;
        }
        else if(filteredList.length = 0){
            item = filteredList[0];
            CommonDropWorth = CommonDropWorth - item.price;
        }
        actualDropList.push(item);
        filteredList = cleanFilteredList.apply;
    }
    return actualDropList;

the idea here was that each opponent has a "CommonDropWorth", which indicates the total value of common drops that this opponent drops.这里的想法是每个对手都有一个“CommonDropWorth”,它表示这个对手掉落的普通掉落物的总价值。 higher value = more drops.更高的价值 = 更多的掉落。 a while loop selects random items until the total value of the drops is close to the value of the "commondropWorth". while 循环选择随机项目,直到掉落的总价值接近“commondropWorth”的值。 items that have a smaller value than the commondropworth are filtered out by the function stored in the "cleanFilteredList" variable.值小于 commondropworth 的项目被存储在“cleanFilteredList”变量中的 function 过滤掉。 now the variable "filteredList" is supposed to assign the return value of the function, but instead the function itself is assigned to it, and due to the type safety tabs this problem is not shown in the IDE. This leads to the following Error: TypeError Cannot read property 'price' of undefined.现在变量“filteredList”应该分配 function 的返回值,但 function 本身被分配给它,并且由于类型安全选项卡,这个问题没有在 IDE 中显示。这导致以下错误: TypeError 无法读取未定义的属性“价格”。 So my question is: how do i assign the var filteredList the return value of the function and not the function itself?所以我的问题是:如何为 var filteredList 分配 function 的返回值而不是 function 本身? Important: The filteredList must be updated every time the commonDropWorth reduces, because otherwise it would make it possible to drop Items that are more worth.重要提示:每次 commonDropWorth 减少时都必须更新 filteredList,否则可能会丢弃更有价值的项目。

Ps, to avoid other unnessesary questions in the future: how can i ensure that my code does not try to asign objects of the wrong type? Ps,为了避免以后出现其他不必要的问题:我如何确保我的代码不会尝试分配错误类型的对象? (Transparency note: i think my post was made invisible, as i made some errors during creation and some of the already given answers disappeared. This is technically a repost, but because of the circumstances, i didnt know how to get an answer on my first post.) (透明说明:我认为我的帖子被隐藏了,因为我在创建过程中犯了一些错误并且一些已经给出的答案消失了。这在技术上是一个转贴,但由于这种情况,我不知道如何在我的第一次发帖。)

I think there is something wrong with your handling of lengths of the array.我认为您对数组长度的处理有问题。

while (filteredList.length > 0){
    let item;
    if (filteredList.length > 1){
        item = filteredList[Math.floor(Math.random()*filteredList.length)];
        CommonDropWorth = CommonDropWorth - item.price;
    }
    else if(filteredList.length = 0){
        item = filteredList[0];
        CommonDropWorth = CommonDropWorth - item.price;
    }
    actualDropList.push(item);
    filteredList = cleanFilteredList.apply;
}
return actualDropList;

Three problems that I can see:我可以看到三个问题:

1. Wrong splitting of cases as 1 vs >1, instead of splitting as 0 versus >0 1. 将案例错误拆分为 1 对 >1,而不是拆分为 0 对 >0

I think you have intended to split up the cases where the length is 1, from the cases where length is >1.我认为您打算将长度为 1 的情况与长度 > 1 的情况分开。 This is not necessary, as the >1 part of the program will work just fine for the ==1 case.这不是必需的,因为程序的 >1 部分在 ==1 情况下工作得很好。

2. Failing to handle the length == 0 case 2. 未能处理长度== 0 的情况

The handling of length == 0 needs to make sure not to select one of the (zero.) items, Remember, when length == 0, you are not allowed to read item #0, just as when length = 10. you are not allowed to read item #10. length == 0 的处理需要确保不是 select 中的一个(零)项目,记住,当 length == 0 时,你不能读取项目 #0,就像 length = 10 时一样。你是不允许阅读第 10 项。

3. I am not sure the ".apply" function works in the way you use it. 3. 我不确定“.apply”function 是否按照您的使用方式工作。

Usually there are parentheses after .apply ?通常在.apply之后有括号?

I usually put the filter in directly where it is needed, unless the function is unusually complicated.我通常将过滤器直接放在需要的地方,除非 function 异常复杂。 Here the boilerplate of creating a named function, and then applying it, is probably too much effort and opportunity for error, I think.这里创建一个名为 function 的样板,然后应用它,我认为可能是太多的努力和错误的机会。

Try this, which fixes items 1 to 3 above.试试这个,它修复了上面的项目 1 到 3。

while (filteredList.length >= 1){
    const item = filteredList[Math.floor(Math.random()*filteredList.length)];
    CommonDropWorth = CommonDropWorth - item.price;
    actualDropList.push(item);
    filteredList = commonItemDataList.filter((Item => Item.price <= CommonDropWorth))
}
 

return actualDropList;

Remember that you can be confident that the block inside the while loop begins with filteredList.length being 1 or more.请记住,您可以确信while循环内的块以filteredList.length为 1 或更大开始。 (I prefer to call it >=1 than >0 , because it helps me remember that it means "if I have at least one item on the list, I can select an item from the list". (我更喜欢称它>=1而不是>0 ,因为它帮助我记住它的意思是“如果我在列表中至少有一个项目,我可以 select 列表中的一个项目”。

Therefore you can get rid of the if (filteredList.length = 0) .因此,您可以摆脱if (filteredList.length = 0) Incidentally, that should have been an == not a single = , but it doesn't matter because you are deleting it.顺便说一句,那应该是==而不是单个= ,但这并不重要,因为您正在删除它。

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

相关问题 如何在javascript中为返回函数的变量赋值 - how to assign a value to a variable of a return function in javascript 如何将javascript事件函数的返回值分配给javascript变量 - how to assign return value of a javascript event function to a javascript variable 时钟角度问题 - &gt;如何调用函数外部的对象并将对象值分配给函数内部变量 - Clock Angle Problem --> How Do I call an Object Outside of a Function and Assign the Object Value to a Variable Inside Function 将函数返回值分配给javascript中的变量 - Assign a function return value to a variable in javascript 如何将javascript函数的返回值分配给python中的变量? - how to assign a javascript function return value to the variable in python? 将函数的返回值赋给变量 - Assign return value of function to variable 如何将异步 function 的返回值分配给变量 - how do I assign a returned value from an async function to a variable 如何为全局变量分配功能? - How do I assign a function to a global variable? 在返回变量之前,我是否必须将 function 结果分配给变量? - Do I have to assign a function result to a variable before I return it? 我可以将ac#变量分配给javascript函数的return语句吗? - Can I assign a c# variable to to the return statement of a javascript function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM