Javascript 如何更改'this'关键字的行为 如何更改下面代码中“this”的行为。当前,它指向HighChart上的单击事件。我希望这里的'this'指的是类中的函数。 你好,ts getPlotBands(){ ..... 点击:功能(e){ 对于(var p=0;p

Javascript 如何更改'this'关键字的行为 如何更改下面代码中“this”的行为。当前,它指向HighChart上的单击事件。我希望这里的'this'指的是类中的函数。 你好,ts getPlotBands(){ ..... 点击:功能(e){ 对于(var p=0;p,javascript,angular,typescript,Javascript,Angular,Typescript,使用箭头函数而不是函数关键字: How to change the behavior of `this' in the below code. Currently, it is pointing to a click event on the HighChart. I want `this` here to refer to function in the class. Hello.ts getPlotBands() { ..... click: function(e) {

使用箭头函数而不是
函数
关键字:

How to change the behavior of `this' in the below code. Currently, it is pointing to a click event on the HighChart. I want `this` here to refer to function in the class.
Hello.ts
 getPlotBands() {
.....
        click: function(e) {
          for(var p = 0; p < this.axis.plotLinesAndBands.length; p++) {
            var currentPlotBand = this.axis.plotLinesAndBands[p]; 
            currentPlotBand.svgElem.attr({fill: currentPlotBand.options.color});
            }
            this.svgElem.attr({fill:'#660066'});
            // this.fromSystemTime = String(new Date(this.options.from));
            this.fromSystemTime = moment(this.options.from).format("MM/DD/YY h:mm:ss");
            this.toSystemTime = String(new Date(this.options.to));
            console.log(moment(this.options.from).format("MM/DD/YY h:mm:ss"));
            (<HTMLInputElement>document.getElementById('fromSys')).value = 
                                      moment(this.options.from).zone("America/Los_Angeles").format('MM/DD/YY HH:mm');
            (<HTMLInputElement>document.getElementById('toSys')).value = 
                                         moment(this.options.to).zone("America/Los_Angeles").format('MM/DD/YY HH:mm');
            console.log('to date -', moment(1556906400000).utcOffset(-420).format('MM/DD/YY HH:mm'));  
            console.log(moment(1556906400000).zone("America/Los_Angeles").format('MM/DD/YY HH:mm'));
            // console.log('from', this.options.from);   
            // console.log('to', this.options.to);     
            console.log(this.options.from + 'and' + this.options.to)
            tagStartTimeForDetailsPage = +moment(this.options.from);
            tagEndTimeForDetailsPage = +moment(this.options.to);
`  this.storedResponse.setTagStartTimeForDetailsPage(+tagStartTimeForDetailsPage);
  this.storedResponse.setTagEndTimeForDetailsPage(+tagEndTimeForDetailsPage); 
        }` }
    I want here `this' to refer my storedResponse service injected in the class not chart obj.

The below 2 lines are giving the error 
`this.storedResponse.setTagStartTimeForDetailsPage(+tagStartTimeForDetailsPage);
  this.storedResponse.setTagEndTimeForDetailsPage(+tagEndTimeForDetailsPage); `

关于这一点,这里有很多问题,但找不到:)

你必须将click函数绑定到类。
Whatever.click.bind(this)
-可能在
Whatever
类的构造函数中(你没有透露
click
是哪个类的成员)您是否尝试创建外部函数并调用它而不是匿名函数?这应该会更改“This”关键字的含义。使用箭头函数:如果使用
单击:(e)=>{
…我无法访问这些行(var p=0;p此回调,您将找到。
click: (e) => {
  // ...
}