在jquery范围内访问和扩展JS原型以在Typescript中使用

时间:2018-01-31 21:56:31

标签: javascript jquery angular typescript

我有一个使用jquery和JavaScript的第三方API。在$(function() {范围内,定义了几个原型。例如,假设这是' Note.js':

$(function() {
"use strict";

var $doc = $(document);
var queryParams = window.ControlUtils.getQueryStringMap();
if (queryParams.getBoolean('hideAnnotationPanel', false)) {
    return;
}

var Note = function(annotation, editable, replyable, deletable) {
    this._editable = editable;
    this._replyable = replyable;
    this._deletable = deletable;
    this._isEditing = false;
    this._stayUncollapsed = false;
    this._annotation = annotation;
    this._parent = null;

    this.$container = $('<div class="noteContainer">');

    this.createContainer();
};

Note.prototype = {
    COLLAPSE_HEIGHT: 120,

    createContainer: function() {
        var me = this;
        var annotation = this.getAnnotation();
        var $container = this.getContainer();
        $container.data('annot', annotation);
        // Continue on...
},
    isEditing: function() {
        return this._isEditing;
    },

    forceUncollapsed: function() {
        this._stayUncollapsed = true;
    },

    unforceUncollapsed: function() {
        this._stayUncollapsed = false;
    }

    // Continue on...
};

$.extend(Note.prototype, window.utils.eventHandler);

What I would like to do, is take all those prototypes and move them to TypeScript Interfaces/Classes so I can extend them and get all the benefits of TS with a clear 'cut' to the 3rd party API.

我已经通过一些教程和SO问题了解了一些方法,但我甚至无法在我的* .ts文件中引用原型。

这样做的最佳方式是什么?

我的最终目标是拥有一个&#39; FooNote.ts&#39;文件类如:

class FooNote extends Note {
    // ...
}

使用类似的东西(不确定是否正确):

interface Note {
    foo(): Note
}

Note.prototype.foo = function(): Note {
    // return some Note here.
}

0 个答案:

没有答案