【问题标题】:why are listed generic functions has overwrite itself on typescript为什么列出的泛型函数已经在打字稿上覆盖了自己
【发布时间】:2019-01-18 16:56:06
【问题描述】:

我正在尝试使用 IOOperation 接口进行一些操作。但我有奇怪的错误。 当我记录操作值或操作结果时,没有问题,但是当我尝试记录接口本身时,我得到了覆盖的操作值和结果。

我的代码在这里:https://stackblitz.com/edit/angular-flpdu1?embed=1&file=src/app/app.component.ts

//app/app.component.ts
import { Component } from '@angular/core';
import { IOperation } from './definitions/operation';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  operations: {
    [operationName: string]: IOperation
  }[] = [];

  columns: {
    columnName: string,
    columnOperations: IOperation[]
  }[] = [];

  constructor() {
    let sum: IOperation = {
      operationName: "sum",
      operationFunction: this.sumFunction
    };
    let avg: IOperation = {
      operationName: "avg",
      operationFunction: this.avgFunction
    };
    this.operations["sum"] = sum;
    this.operations["avg"] = avg;
    this.columns = [
      {
        columnName: "column1",
        columnOperations: [this.operations["sum"]]
      },
      {
        columnName: "column2",
        columnOperations: [this.operations["avg"]]
      },
      {
        columnName: "column3",
        columnOperations: []
      },
      {
        columnName: "column4",
        columnOperations: [this.operations["sum"], this.operations["avg"]]
      },
      {
        columnName: "column5",
        columnOperations: []
      }
    ];
    this.doOperation();
  }
  sumFunction(values: number[]): number {
    let result: number = 0;
    values.forEach((i: number) => {
      result += i;
    });
    return result;
  }
  avgFunction(values: number[]): number {
    let result: number = 0;
    values.forEach((i: number) => {
      result += i;
    });
    return result / values.length;
  }
  doOperation(): void {
    let values: number[][] = [
      [1, 2, 3, 4, 5],
      [6, 7, 8, 9, 10],
      [11, 12, 13, 14, 15],
      [16, 17, 18, 19, 10],
      [21, 22, 23, 24, 25],
      [26, 27, 28, 29, 20],
      [31, 32, 33, 34, 35]
    ];
    for (let i = 0; i < this.columns.length; i++) {
      for (let k = 0; k < this.columns[i].columnOperations.length; k++) {
        console.log(i+","+k);

        let gc: number[] = [];
        let gcOperation:IOperation=this.columns[i].columnOperations[k];

        for (let j = 0; j < values.length; j++) { gc.push(values[j][i]); }

        gcOperation.operationValues = gc;

        gcOperation.operationResult = gcOperation.operationFunction(gcOperation.operationValues);
        console.log("correct answer:"+gcOperation.operationResult);//correct answer
        console.log(gcOperation);
        //gcOperation.operationResult is wrong here
      }
    }
  }
}

// /definitions/operation.ts
export interface IOperation {
  operationName: string,
  operationFunction?: Function,
  operationValues?: any[],
  operationResult?: any
}

正如您在控制台中看到的那样,我记录了 operation.operationResult 并且它是正确的。然后我立即记录相同的“操作”并且它有错误 operationResult.

我正在使用opera 54.0chrome 68.0。 如果你没有同样的问题(如果它只是为了我的电脑)

there is what i see

(对不起,如果我违反任何网站规则,这是我的第一个问题,我的英语不是很好。谢谢大家愿意帮助我)。

【问题讨论】:

  • 在 macOS 上的 chrome 68 上测试。日志没有问题
  • 请直接将代码相关部分添加到问题中,不要链接到外部资源。外部链接往往会被破坏,这使得问题对未来的读者毫无用处。

标签: angular typescript generics


【解决方案1】:

operationResult 的值可能在代码执行期间发生变化。在某个时候它是 112 - 稍后它是 133。

由于您记录 对象引用(不是深拷贝),当将对象记录到控制台时,很容易发生这种奇怪的行为。 虽然顶部的对象表示是您登录时的快照,但“打开”对象后的值是当前值。

您可能想要记录对象的深层副本以验证我的假设(例如通过使用 lodash/jquery 的相应函数)

【讨论】:

  • 谢谢你。我正在使用 let newObject=Object.assing({},oldObject);深拷贝是我的英雄。 ( //最后一节 ... let gc: number[] = []; let gcOperation:IOperation=Object.assign({},this.columns[i].columnOperations[k]); for (let j = 0; j
猜你喜欢
  • 2020-05-26
  • 2023-02-22
  • 2020-12-16
  • 1970-01-01
  • 1970-01-01
  • 2020-01-22
  • 2020-06-13
  • 2022-11-11
  • 2023-02-14
相关资源
最近更新 更多