抽象方法TypeScript的怪异行为



我在这里使用带有引用"大气.d.ts"源的typescript。我用抽象方法触发了一个奇怪的行为,导致了错误:

TypeError:this.protectedMethod不是函数

这是打字代码:

/// <reference path="../atmosphere.d.ts" />
import Request = Atmosphere.Request;
abstract class AbstractRequest {
    // The atmosphere request
    protected socket: Request;
    // Here we initialize the socket
    protected init(url: string): void {
        this.socket = {
            url                 : "http://localhost:9000/" + url,
            contentType         : "application/json",
            transport           : "websocket" ,
            fallbackTransport   : "long-polling"
        };
        /* SOME CODE */
        this.socket.onOpen = function(response) {
            this.protectedMethod();
        };
    }
    // Some protected method called in this.socket.onOpen
    protected abstract protectedMethod(): void;
}
class Registration extends AbstractRequest {
    // Implementation of the abstract method
    protected protectedMethod(): void {
        console.log("hello");
    }
}

在没有错误的情况下,生成以下javascript代码:

var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var AbstractRequest = (function () {
    function AbstractRequest() {
    }
    // Here we initialize the socket
    AbstractRequest.prototype.init = function (url) {
        this.socket = {
            url: "http://localhost:9000/" + url,
            contentType: "application/json",
            transport: "websocket",
            fallbackTransport: "long-polling"
        };
        /* SOME CODE */
        this.socket.onOpen = function (response) {
            this.protectedMethod();
        };
    };
    return AbstractRequest;
}());
var Registration = (function (_super) {
    __extends(Registration, _super);
    function Registration() {
        _super.apply(this, arguments);
    }
    // Implementation of the abstract method
    Registration.prototype.protectedMethod = function () {
        console.log("hello");
    };
    return Registration;
}(AbstractRequest));
//# sourceMappingURL=test.js.map

当我实现"onOpen"方法时,我不能从"socket"变量调用抽象方法(也可能是非抽象的?)。我现在找到的唯一解决方法是实例化一个全局变量

var registration = new Registration();

然后:

this.socket.onOpen = function(response) {
    registration.protectedMethod;
};

有了这个变通方法,我必须将"protectedMethod"定义为public。有没有对这种行为的解释,以及解决方法/解决方案?顺便说一句,我使用的是1.8.10 打字脚本

谢谢,

这是由于this在JavaScript:中的工作方式

var AbstractRequest = (function () {
    function AbstractRequest() { }
    AbstractRequest.prototype.init = function (url) {
        // ...snip...
        this.socket.onOpen = function (response) {
            this.protectedMethod();
        };
    };
    return AbstractRequest;
}());

当您调用new AbstractRequest().socket.onOpen()时,this将绑定到socket而不是new AbstractRequest()this指向点左侧的任何位置)。

您可以使用箭头函数来解决此问题。在箭头函数中,this绑定到它在其中定义的上下文,而不是它在其中运行的上下文:

this.socket.onOpen = response => {
    this.protectedMethod();
};

相关内容

  • 没有找到相关文章

最新更新