簡體   English   中英

抽象方法TypeScript的怪異行為

[英]Weird behaviour with abstract method TypeScript

在此處將打字稿與參考“ atmosphere.d.ts” 源一起使用 我用抽象方法觸發了一個奇怪的行為,這導致了錯誤:

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”公共。 是否有對此行為的解釋以及解決方法/修復程序? 順便說一句,我使用打字稿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將被綁定到socketnew AbstractRequest() this指向無論是在點的左邊)。

您可以使用箭頭功能來解決此問題。 在箭頭的功能this勢必會有人在,沒有它正在被運行的背景下定義的上下文:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM