简体   繁体   English

为什么此Typescript / jscript代码段的范围出错

[英]Why the scope goes wrong in this Typescript/jscript piece of code

the code below is inside a "view" and is having scope issues with "this" 下面的代码在“视图”内部,并且存在“ this”的范围问题

Typescript: 打字稿:

getInvitations() {
        var invitationsUri = "/Api/Invitations/";

        $.ajax({
            url: invitationsUri ,
            type: 'GET',
            dataType: 'json',
            contentType: 'application/json'
        })
            .done((result) => {
                $.each(result, (index, item) => {
                    var invitation = new Invitation();
                    invitation.InvitationId(item.InvitationId);
                    invitation.Invited(true);
                    invitation.Email(item.Email);

                    this.Business.Invitations.push(invitation);
                });
            })
            .fail((x, y, z) => {
                alert(x + '\n' + y + '\n' + z + ' - failed to get invitations');
            });
    }

    getUsers() {
        var usersUri = "/Api/Users/";

        $.ajax({
            url: usersUri,
            type: 'GET',
            dataType: 'json',
            contentType: 'application/json'
        })
            .done((result) => {
                $.each(result, (index, item) => {
                    var user = new User();
                    user.Id(item.Id);
                    user.UserName(item.UserName);
                    user.Email(item.Email);

                    this.Business.Users.push(user);
                });
            })
            .fail((x, y, z) => {
                alert(x + '\n' + y + '\n' + z + ' - failed to get users');
            });
    }

The first method works well, the second thinks "this" is referring to "item", so it does not recognize "Business" as a property and with that the objective is not achieved. 第一种方法效果很好,第二种方法则认为“ this”是指“ item”,因此它无法将“ Business”视为一种财产,因此无法实现目标。

Any ideas what could be happening here? 有什么想法会在这里发生吗?

EDIT: Here is the generated code (jscript) 编辑:这是生成的代码(jscript)

BusinessView.prototype.getInvitations = function () {
        var _this = this;
        var invitationsUri = "/Api/Invitations/";

        $.ajax({
            url: invitationsUri,
            type: 'GET',
            dataType: 'json',
            contentType: 'application/json'
        }).done(function (result) {
            $.each(result, function (index, item) {
                var invitation = new Invitation();
                invitation.InvitationId(item.InvitationId);
                invitation.Invited(true);
                invitation.Email(item.Email);

                _this.Business.Invitations.push(invitation);
            });
        }).fail(function (x, y, z) {
            alert(x + '\n' + y + '\n' + z + ' - failed to get invitations');
        });
    };

    BusinessView.prototype.getUsers = function () {
        var _this = this;
        var usersUri = "/Api/Users/";

        $.ajax({
            url: usersUri,
            type: 'GET',
            dataType: 'json',
            contentType: 'application/json'
        }).done(function (result) {
            $.each(result, function (index, item) {
                var user = new User();
                user.Id(item.Id);
                user.UserName(item.UserName);
                user.Email(item.Email);

                _this.Business.Users.push(user);
            });
        }).fail(function (x, y, z) {
            alert(x + '\n' + y + '\n' + z + ' - failed to get users');
        });
    };

Inside a jQuery .each() callback, this is always set by jQuery so it won't be what this was in a higher level scope. 里面一个jQuery .each()回调, this总是由jQuery的设置,所以它不会是什么this是在一个较高的水平范围。 If you need to access the value of this from the higher level scope, then the usual work-around is to save it to a local variable at that higher level scope and you can then access that local variable from within the .each() callback function. 如果您需要访问的价值this从更高的层次范围,那么通常的解决办法是将其保存到一个局部变量在这个更高的水平范围,那么你就可以访问来自内部的局部变量.each()回调功能。

From the code snippet you included in your answer, it is not clear what this value you want to access. 从代码段包括你在你的答案,但目前尚不清楚this ,你要能够访问值。 You may also need to know that call a regular function such as calling getInvitations() will change the this pointer to point to either the global object or in strict mode to undefined . 您可能还需要知道,调用常规函数(如调用getInvitations()会将this指针更改为指向全局对象或在严格模式下指向undefined this is set by the javascript interpreter on every function call based on how the function call is made. this是由javascript解释器根据每个函数调用的方式在每个函数调用上设置的。

您在每个调用中都得到正确的lambda值,即$.each(result, (index, item) => {您应该对函数定义执行相同的操作,以确保在整个过程中, this的作用域都是正确的,即getInvitations = () => {getUsers = () => {

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

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