函数定义不是函数错误角4

时间:2018-04-01 12:08:03

标签: javascript angular

我正在尝试编写一个函数,并在高亮度工具提示的另一个函数中调用它。

isMillionNumber = this.validateMillionNumber(points[0].point.high);

我收到一条错误消息,说明validateMillionNumber不是函数。不确定我做错了什么?范围或声明是否有问题。我在网上访问了几个例子,但无法理解出了什么问题

import {
  Component,
  Input,
  OnInit
} from '@angular/core';
import {
  ShortNumberFormatPipe
} from '@wtw/toolkit';

@Component({
  selector: 'app-box-plot-chart',
  template: '<chart [options]="options" (load)="getInstance($event.context)"></chart>',
  styles: [`
            chart{
                  display: block;
                  width: 100% !important;
                  padding:0;
            }
        `]
})

export class BoxPlotChartComponent implements OnInit {

  static chart(shortNumberFormatPipe: ShortNumberFormatPipe, moduleName: string) {
    return {

      chart: {
        type: 'boxplot'
      },

      title: {
        text: ''
      },
      legend: {
        enabled: false
      },
      credits: {
        enabled: false
      },
      yAxis: {
        title: {
          text: ''
        },
        plotLines: false
      },
      tooltip: {
        shared: true,
        useHTML: true,



        formatter: function() {
          let value: any;
          let txt: any;
          let isMillionNumber: false;

          if (moduleName === 'npv') {
            let points = this.points;
            isMillionNumber = this.validateMillionNumber(points[0].point.high);

            txt = '<strong style="font-size:12px;color:' + points[0].point.color + '">' + points[0].point.name + '</strong><br><br><table>';

            if (isMillionNumber)
              value = shortNumberFormatPipe.transform(points[0].point.high, 2);
            else
              value = shortNumberFormatPipe.transform(points[0].point.high, 0);

            txt += '<tr><td  style="font-size:10px;">Maximum: </td>';
            txt += '<td style="font-size:10px;"><b>' + value + '</b></td></tr>';
            value = shortNumberFormatPipe.transform(points[0].point.q1, 0);
            txt += '<tr><td  style="font-size:10px;">Upper quartile: </td>';
            txt += '<td style="font-size:10px;"><b>' + value + '</b></td></tr>';
            value = shortNumberFormatPipe.transform(points[0].point.median, 0);
            txt += '<tr><td  style="font-size:10px;">Median: </td>';
            txt += '<td style="font-size:10px;"><b>' + value + '</b></td></tr>';
            value = shortNumberFormatPipe.transform(points[0].point.q3, 0);
            txt += '<tr><td  style="font-size:10px;">Lower quartile: </td>';
            txt += '<td style="font-size:10px;"><b>' + value + '</b></td></tr>';
            value = shortNumberFormatPipe.transform(points[0].point.low, 0);
            txt += '<tr><td  style="font-size:10px;">Minimum: </td>';
            txt += '<td style="font-size:10px;"><b>' + value + '</b></td></tr>';
            txt += '</table>';
            return txt;
          } else if (moduleName === 'eva') {
            let points = this.points,
              txt = '<strong style="font-size:12px;color:' + points[0].point.color + '">' + points[0].point.name + '</strong><br><br><table>',
              value = shortNumberFormatPipe.transform(points[0].point.high, 2);
            txt += '<tr><td  style="font-size:10px;">Maximum: </td>';
            txt += '<td style="font-size:10px;"><b>' + value + '</b></td></tr>';
            value = shortNumberFormatPipe.transform(points[0].point.q1, 2);
            txt += '<tr><td  style="font-size:10px;">Upper quartile: </td>';
            txt += '<td style="font-size:10px;"><b>' + value + '</b></td></tr>';
            value = shortNumberFormatPipe.transform(points[0].point.median, 2);
            txt += '<tr><td  style="font-size:10px;">Median: </td>';
            txt += '<td style="font-size:10px;"><b>' + value + '</b></td></tr>';
            value = shortNumberFormatPipe.transform(points[0].point.q3, 2);
            txt += '<tr><td  style="font-size:10px;">Lower quartile: </td>';
            txt += '<td style="font-size:10px;"><b>' + value + '</b></td></tr>';
            value = shortNumberFormatPipe.transform(points[0].point.low, 2);
            txt += '<tr><td  style="font-size:10px;">Minimum: </td>';
            txt += '<td style="font-size:10px;"><b>' + value + '</b></td></tr>';
            txt += '</table>';
            return txt;
          }


        }
      },
      series: []
    };



  }





  public options: any;
  chart: any;
  @Input() public series: any;
  @Input() public moduleName: string = '';
  private shortNumberFormatPipe = new ShortNumberFormatPipe();

  constructor() {


  }

  ngOnInit() {

    this.options = BoxPlotChartComponent.chart(this.shortNumberFormatPipe, this.moduleName);
  }

  getInstance(chartInstance): void {
    this.chart = chartInstance;
    this.redraw();
  }


  ngOnChanges(data: any) {
    if (!data.series.currentValue || !this.chart) return;
    data.series.currentValue.map(s => {
      this.chart.addSeries(s);
    });
    this.chart.reflow();
  }

  redraw() {
    if (!this.chart) return;
    this.chart.addSeries(this.series);
  }



}


function validateMillionNumber(millionNumber: number) {
  var number = millionNumber.toString()

  //check of the number contains decimals

  if (number.indexOf(".") === -1) {
    number = number.toString().split(".")[0];
    // check the length of the number
    if (number.length > 7)
      return true;
    else
      return false;
  } else {
    if (number.length > 7)
      return true;
    else
      return false;
  }

}

2 个答案:

答案 0 :(得分:2)

您应该使用TypeScript支持的IDE。我推荐Visual Studio Code。在您的代码中,删除this并将其写为:

isMillionNumber = validateMillionNumber(points[0].point.high);

我希望这会有所帮助。 :)

答案 1 :(得分:0)

从函数调用中删除 this。 validateMillionNumber 不是类成员函数,而是外部函数。我相信在引用类封装成员时应该使用 this。

所以,你的电话应该是这样的:

isMillionNumber = validateMillionNumber(points[0].point.high);