简体   繁体   English

在 TypeScript 中使用箭头函数:如何使它们成为类方法?

[英]Using arrow functions in TypeScript: how to make them class methods?

I'm fairly experienced programming but quite new to TypeScript.我的编程经验相当丰富,但对 TypeScript 还很陌生。

Trying to use it with jQuery and immediately ran into the 'this' issue with callbacks (such as $(document).ready.尝试将它与 jQuery 一起使用并立即遇到回调的“this”问题(例如 $(document).ready。

Using $.proxy() is one way to go, but using TypeScript's arrow (lambda) functions seemed much better.使用 $.proxy() 是一种方法,但使用 TypeScript 的箭头 (lambda) 函数似乎要好得多。 But I only see them used as expressions — that is the entire function is defined inline.但我只看到它们用作表达式——也就是说,整个函数都是内联定义的。 I would like to be able to set up arrow functions that could be called as methods of my class, something like (in pseudocode):我希望能够设置可以作为我的类的方法调用的箭头函数,类似于(伪代码):

class Something {
    constructor() {
    $(' nav li').click(this.menuClick);
    }
 private menuClick (and this would be an arrow function to preserve 'this')()=>    
  // it would call this.anotherMethod()
  // plus lots of other things so it won't easily fit inline

 private anotherMethod():void {
        // do something on the menu click, several lines perhaps, 
  }

}

I come from an OOP background in AS3, and that's how I was able to do it -- 'this' was readily accessible or it was clear how to access it.我来自 AS3 的 OOP 背景,这就是我能够做到这一点的方式——“this”很容易访问,或者很清楚如何访问它。 I'm keen to use TypeScript to get past the OOP hurdle I'm having with Javascript plain -- but it seems cumbersome (to me) to have to proxy all the jQuery calls (I know there are classes out there to do this for me, but is there a simpler way, with arrow/lambda functions?).我热衷于使用 TypeScript 来克服我在使用 Javascript 时遇到的 OOP 障碍——但是(对我来说)必须代理所有 jQuery 调用似乎很麻烦(我知道有一些类可以为我,但是有没有更简单的方法,使用箭头/lambda 函数?)。

Have patience if I'm not grokking the obvious, but it ain't obvious to me!如果我不是很明显,请耐心等待,但这对我来说并不明显!

The reason there isn't a way to do this by default is that it's a tremendous runtime cost in terms of per-instance memory consumption if done automatically.默认情况下没有办法执行此操作的原因是,如果自动完成,就每个实例的内存消耗而言,这是一个巨大的运行时成本。 Rather than having one base prototype object with all the functions in it and each class instance pointing to that prototype for its function definitions, you end up having a closure per function for every instance of the class.与其拥有一个包含所有函数的基本原型对象,并且每个类实例都指向该原型的函数定义,不如为类的每个实例的每个函数创建一个闭包。

There might be better type system support for this in the future, but for now, dealing with this binding issues is just a tax you have to pay when writing JavaScript of any flavor.将来可能会有更好的类型系统支持,但就目前而言,处理this绑定问题只是编写任何风格的 JavaScript 时必须支付的税。 Hopefully as things like TypeScript gain momentum, library authors will reduce their usage of this -stomping (though the DOM will never have that luxury).希望随着 TypeScript 之类的东西获得动力,库作者将减少他们对this -stomping 的使用(尽管 DOM 永远不会有这种奢侈)。

The upside is that in TypeScript, it's not a huge amount more code when you do need to re-re-bind this :好处是,在 TypeScript 中,当您确实需要重新绑定this时,代码量不会增加很多:

$(' nav li').click(() => this.menuClick());

or要么

$(' nav li').click(this.menuClick.bind(this));

The short answer:简短的回答:

If you change: $(' nav li').click(this.menuClick);如果更改: $(' nav li').click(this.menuClick);

into: $(' nav li').click(() => this.menuClick());进入: $(' nav li').click(() => this.menuClick());

then this will be your class 'Something' inside the menuClick method.那么this将是menuClick方法中的类“Something”。

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

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