如何正确地在循环外创建函数

huangapple go评论66阅读模式
英文:

How to make a function outside the loop properly

问题

I've created a collection with collections inside and loop them. And I want to have one function outside the loops to call it in many cycles after it.

在创建了一个包含嵌套集合的集合并对它们进行循环后,我希望在循环之外有一个函数,可以在之后的多个周期中调用它。

In the beginning of the script I declare the variables for function to use,

在脚本的开头,我声明了函数要使用的变量,

(function SplitByPalletsV2() {
let MPkg;
let MQty = 0;
///then I create a map
(在这里我创建一个映射)
let Pkg = new Map();
let PQty = 0;
Pkg.clear();
///... (some code for filling collection)
////then I loop other collection called Waybill and inside this loop the map is filled :
(然后我循环另一个名为Waybill的集合,在这个循环内部填充了映射:)
for (let [key, value] of Waybill) {
///...fill the Pkg map
///somewhere there I call the function to find max of value in this map
(在某个地方,我调用了函数来查找这个映射中的值的最大值)
MaxPackage();
}
function MaxPackage() {
for (let [key6, value6] of Pkg) {
if (Number(value6) > MQty) {
MQty = Number(value6);
MPkg = key6;
}
}
}
})();

And it doesn't work.

但它不起作用。

When I declare map and variables inside the collection and put a function inside the Waybill loop like this, it works:

当我在集合内部声明映射和变量,并像这样将函数放在Waybill循环内部时,它可以工作:

let Pkg = new Map();
let PQty = 0;
Pkg.clear();
//filling the Pkg
(填充Pkg)

let MPkg;
let MQty = 0;
function MaxPackage() {
for (let [key6, value6] of Pkg) {
if (Number(value6) > MQty) {
MQty = Number(value6);
MPkg = key6;
}
}
}
MaxPackage();

Though I write the code in a built-in IDE for OnlyOffice macros, I'm very limited with testing and debug instruments (no instruments at all), I don't understand what happens -seems when I declare global variables it doesn't work.

尽管我在OnlyOffice宏的内置IDE中编写了代码,但在测试和调试工具方面非常有限(几乎没有),我不太明白发生了什么 - 似乎当我声明全局变量时它不起作用。

英文:

I've created a collection with collections inside and loop them. And I want to have one function outside the loops to call it in many cycles after it.

In the beginning of the script I declare the variables for function to use,

(function SplitByPalletsV2() {
  let MPkg;
  let MQty = 0;
  ///then I create a map

  let Pkg = new Map();
  let PQty = 0;
  Pkg.clear();
  ///... (some code for filling collection)
  ////then I loop other collection called Waybill and inside this loop the map is filled  :
  for (let [key, value] of Waybill) {
    ///...fill the Pkg map
    ///somewhere there I call the function to find max of value in this map
    MaxPackage();
  }
  function MaxPackage() {
    for (let [key6, value6] of Pkg) {
      if (Number(value6) > MQty) {
        MQty = Number(value6);
        MPkg = key6;
      }
    }
  }
})();

And it doesn't work.

When I declare map and variables inside the collection and put a function inside the Waybill loop like this, it works:

let Pkg = new Map();
let PQty = 0;
Pkg.clear();
//filling the Pkg

let MPkg;
let MQty = 0;
function MaxPackage() {
  for (let [key6, value6] of Pkg) {
    if (Number(value6) > MQty) {
      MQty = Number(value6);
      MPkg = key6;
    }
  }
}
MaxPackage();

Though I write the code in a built-in IDE for OnlyOffice macros, I'm very limited with testing and debug instruments (no instruments at all), I don't understand what happens -seems when I declare global variables it doesn't work.

答案1

得分: 0

问题解决似乎是我声明了MPkg的方式。
我将MPkg声明为let MPkg;,但在let MPkg=0;之后开始工作。
不明白为什么,但就是这样。

英文:

The problem solved seemed in the way I declared MPkg.
I declared MPkg as let MPkg; but started to work after let MPkg=0;
Don't understand why, but it's so.

huangapple
  • 本文由 发表于 2023年5月26日 14:48:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76338276.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定