简体   繁体   English

如何使用Typescript将类变量传递到函数内部的函数中?

[英]How can I pass a class variable into a function inside a function with typescript?

I created a Modal class like this: 我创建了一个Modal类,如下所示:

export class Modal {
        link: Link;
        constructor (link: Link) {
            this.link = link;
        }
        create() {
            this.link.Modal.$Modal = $.modal({ });
        }
        addSubmit() {
            this.link.Modal.$Form.submit(function () {
                var a = this.link;
                submitHandler(this.link);
                return false;
            });
        }
}

Here is the code that I am using: 这是我正在使用的代码:

var modal = new Modal(link);
modal.create();
modal.addSubmit();

There is a problem here. 这里有个问题。 I want to pass the link as a parameter to the submitHandler function. 我想将链接作为参数传递给SubmitHandler函数。 However it seems it's not there. 但是似乎不存在。 Also when I check everthing on the line "var a = this.link" it seems that the variable "a", the "this" and the "link" all show as "any" when I hover over them. 同样,当我检查“ var a = this.link”行上的所有内容时,将鼠标悬停在变量“ a”,“ this”和“ link”上时,它们似乎都显示为“ any”。

You have discovered JavaScript scope - which can be confusing at first. 您已经发现JavaScript范围-刚开始时可能会造成混淆。

JavaScript is functionally scoped. JavaScript具有功能范围。 This means that the variables belong to the function they are declared in. 这意味着变量属于声明它们的函数。

When the variable is declared outside the function, JavaScript attempts to walk up the chain to find the variable. 当在函数外部声明变量时,JavaScript会尝试沿链向上查找该变量。

This is confusing when the context you type the code in is not the context your code is called in. In your case, this in the context of the submit function is the element that is being submitted (a form element for example). 当上下文您键入是不是你的代码是所谓的上下文有关的代码,这是令人困惑的。在你的情况, this在提交函数的上下文是正在提交的元素(例如一个表单元素)。 It isn't the class where you write the submit function. 它不是您编写Submit函数的类。

Here is an update that should work: 这是应该起作用的更新:

export class Modal {
    link: Link;
    constructor (link: Link) {
        this.link = link;
        this.link.Modal.$Modal = $.modal({ });
    }

    addSubmit() {
        var _this = this;
        this.link.Modal.$Form.submit(function () {
            var a = _this.link;
            submitHandler(_this.link);
            return false;
        });
    }
}

In this example we put this into a variable with a name that won't be overridden so the JavaScript runtime won't stop looking until it finds it. 在此示例中,我们将this放入一个变量,该变量的名称不会被覆盖,因此JavaScript运行时不会停止查找,直到找到它为止。 (It will stop looking for this straight away as it will have it defined locally as the form element). (它将停止寻找this直线距离,因为它会在本地定义表单元素)。 As there is no local _this JavaScript will walk up the chain until it discovers our variable, which represents the class. 由于没有本地_this JavaScript会沿链走,直到发现代表类的变量。

There is also a short-hand in TypeScript to do this, which results in the same JavaScript as we have hand-written above: TypeScript中还有一个简写方式可以完成此操作,从而产生与上面手写的相同的JavaScript:

export class Modal {
    constructor (public link: Link) {
        this.link.Modal.$Modal = $.modal({ });
    }

    addSubmit() {
        this.link.Modal.$Form.submit(() => {
            var a = this.link;
            submitHandler(this.link);
            return false;
        });
    }
}

Lastly, you don't want calling code to have to rely on calling functions in a specific order for things to work. 最后,您不希望调用代码必须依靠特定顺序的调用函数才能正常工作。 This is an example of exposing the internal workings of your class, so I have updated the usage of the class to be: 这是暴露类内部工作的一个示例,因此我将类的用法更新为:

var modal = new Modal(link);
modal.addSubmit();

I am assuming that addSubmit is optional - otherwise the Modal class should do that too without the calling code having to know about it. 我假设addSubmit是可选的-否则Modal类也应该这样做,而调用代码不必知道它。

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

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