简体   繁体   English

在对象内部定义一个函数,并将变量作为函数的一部分

[英]Define a Function inside an Object with a variable as part of the function

I am trying to define a function inside an object where one item the portion of the function to be processed is based on a variable. 我正在尝试在对象内部定义一个函数,其中要处理的函数部分中的一项基于变量。 In the code below, how do I get *dynamicValue* to be saved as the current value of myColumn ? 在下面的代码中,如何将*dynamicValue*保存为myColumn的当前值?

  var viewColumns = [0];
  var count = 0;
  var dataCount = 3;
  var total = 0;
  while (count < dataCount) {
    var myColumn = count + 1;
    var viewColumnDescription = {
        calc: function (dt, row) {
          return dt.getValue(row, *dynamicValue*);
        },
        type: "number",
        role: "annotation"
      };
    viewColumns.push(myColumn);
    viewColumns.push(viewColumnDescription);
    count += 1;
  }

Note that I have hard coded a value for dataCount so that all variables are defined in the sample section but in my actual code this value currently changes to be a number between 1 to 3. It could be more at a later date as well so I want to be able to use a loop to define the items. 请注意,我已经为dataCount硬编码了一个值,以便在示例部分中定义所有变量,但是在我的实际代码中,该值当前更改为1到3之间的一个数字。以后可能还会更多。希望能够使用循环来定义项目。

In this case the resulting object should be as if I did this: 在这种情况下,生成的对象应该就像我这样做:

viewColumns = [0,
      1, {
        calc: function (dt, row) {
          return dt.getValue(row, 1);
        },
        type: "number",
        role: "annotation"
      },
      2, {
        calc: function (dt, row) {
          return dt.getValue(row, 2);
        },
        type: "number",
        role: "annotation"
      },
      3, {
        calc: function (dt, row) {
          return dt.getValue(row, 3);
        },
        type: "number",
        role: "annotation"
      },
      {
        calc: function (dt, row) {
          return 0;
        },
        label: "Total",
        type: "number",
      },
      {
        calc: function (dt, row) {
          return 'Total: ' + (dt.getValue(row, 1) 
                           + dt.getValue(row, 2) 
                           + dt.getValue(row, 3));
        },
        type: "string",
        role: "annotation"
      }
    ];

If dataCount were 2 instead of 3, all references to dt.getValue(row, 3) would be removed. 如果dataCount是2而不是3,则将删除对dt.getValue(row, 3)所有引用。 Hopefully once I know the first part I can figure out how to have the last function calculate the total of all available rows. 希望一旦我知道了第一部分,我就能弄清楚如何让最后一个函数计算所有可用行的总数。

This is to be used to create charts using the Google Visualization API. 这将用于使用Google Visualization API创建图表。

Change myColumn and viewColumnDescription from var into let . myColumnviewColumnDescriptionvar更改为let Replace *dynamicValue* with myColumn myColumn替换*dynamicValue*

Or if you don't want to use let , you'll have to use closures to make sure that the value of myColumn is stored in the appropriate scope... 或者如果您不想使用let ,则必须使用闭包以确保myColumn的值存储在适当的范围内...

If you don't use let - or you don't put it in a closure, all of the viewColumnDescription functions would see myColumn with value = 3 如果您不使用viewColumnDescription或不将其放在闭包中,则所有viewColumnDescription函数都将看到myColumn的值= 3

Sample with let - note that I've used it only where strictly necessary. let示例-请注意,仅在严格必要的情况下才使用它。

  var viewColumns = [0];
  var count = 0;
  var dataCount = 3;
  var total = 0;
  while (count < dataCount) {
    let myColumn = count + 1;
    let viewColumnDescription = {
        calc: function (dt, row) {
          return dt.getValue(row, myColumn);
        },
        type: "number",
        role: "annotation"
      };
    viewColumns.push(myColumn);
    viewColumns.push(viewColumnDescription);
    count += 1;
  }

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

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